
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Windows 批处理脚本学习教程 - MonianHello - Blog</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="MonianHello,"> 
    <meta name="description" content="


第一章：前言
第二章：显示篇
第三章：赋值 调用 参数
第四章：条件 循环
第五章：组合命令 管道命令
第六章：常用实例 上
第七章：常用实例 下
第八章：番外篇



Collected b,"> 
    <meta name="author" content="MonianHello"> 
    <link rel="alternative" href="atom.xml" title="MonianHello - Blog" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
<link rel="stylesheet" href="/css/diaspora.css">

	<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-8691406134231910",
              enable_page_level_ads: true
         });
    </script>
    <script async custom-element="amp-auto-ads"
        src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
    </script>
<meta name="generator" content="Hexo 5.2.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">MonianHello - Blog</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://monianhello.github.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">Windows 批处理脚本学习教程</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">Windows 批处理脚本学习教程</h1>
        <div class="stuff">
            <span>十一月 24, 2020</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/%E5%AD%A6%E4%B9%A0/" rel="tag">学习</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/%E6%89%B9%E5%A4%84%E7%90%86/" rel="tag">批处理</a></li></ul>


        </div>
        <div class="content markdown">
            <!-- toc -->

<ul>
<li><a href="#%E7%AC%AC%E4%B8%80%E7%AB%A0%E5%89%8D%E8%A8%80">第一章：前言</a></li>
<li><a href="#%E7%AC%AC%E4%BA%8C%E7%AB%A0%E6%98%BE%E7%A4%BA%E7%AF%87">第二章：显示篇</a></li>
<li><a href="#%E7%AC%AC%E4%B8%89%E7%AB%A0%E8%B5%8B%E5%80%BC-%E8%B0%83%E7%94%A8-%E5%8F%82%E6%95%B0">第三章：赋值 调用 参数</a></li>
<li><a href="#%E7%AC%AC%E5%9B%9B%E7%AB%A0%E6%9D%A1%E4%BB%B6-%E5%BE%AA%E7%8E%AF">第四章：条件 循环</a></li>
<li><a href="#%E7%AC%AC%E4%BA%94%E7%AB%A0%E7%BB%84%E5%90%88%E5%91%BD%E4%BB%A4-%E7%AE%A1%E9%81%93%E5%91%BD%E4%BB%A4">第五章：组合命令 管道命令</a></li>
<li><a href="#%E7%AC%AC%E5%85%AD%E7%AB%A0%E5%B8%B8%E7%94%A8%E5%AE%9E%E4%BE%8B-%E4%B8%8A">第六章：常用实例 上</a></li>
<li><a href="#%E7%AC%AC%E4%B8%83%E7%AB%A0%E5%B8%B8%E7%94%A8%E5%AE%9E%E4%BE%8B-%E4%B8%8B">第七章：常用实例 下</a></li>
<li><a href="#%E7%AC%AC%E5%85%AB%E7%AB%A0%E7%95%AA%E5%A4%96%E7%AF%87">第八章：番外篇</a></li>
</ul>
<!-- tocstop -->

<p>Collected by <a target="_blank" rel="noopener" href="http://30c.org/">Clove</a></p>
<h2><span id="第一章前言">第一章：前言</span></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line">1.1 什么是批处理？</span><br><span class="line"></span><br><span class="line">从内容上看，批处理文件包含了大量的基本DOS命令，是一种可执行文件。该文件运行时能按照其规则将其中的命令逐一执行。使用批处理文件进行的批量的命令处理的过程，称之为批处理。</span><br><span class="line">批处理文件(Batch File，简称BAT文件)是一种在DOS下最常用的可执行文件。它具有灵活的操纵性，可适应各种复杂的计算机操作。所谓的批处理，就是按规定的顺序自动执行若干个指定的DOS命令或程序。即是把原来一个一个执行的命令汇总起来，成批的执行，而程序文件可以移植到其它电脑中运行，因此可以大大节省命令反复输入的繁琐。同时批处理文件还有一些编程的特点，可以通过扩展参数来灵活的控制程序的执行，所以在日常工作中非常实用。[1]</span><br><span class="line"></span><br><span class="line">批处理文件的后缀名为.bat(Batch的缩写)。可以用绝大多数的文本编辑工具进行编辑。由于批处理文件的实质就是一个命令的集合，所以批处理文件的工作平台是由这些命令所工作的平台来决定，如今Microsoft环境下的批处理文件(.bat和.cmd文件)的平台，当然也就是Microsoft的DOS和Windows系统。[1]</span><br><span class="line"></span><br><span class="line">1.2 了解与使用基本DOS命令</span><br><span class="line"></span><br><span class="line">说到批处理，我们不得不先讨论一些基本的DOS命令。对于“命令”(<span class="string">&quot;Command&quot;</span>)一词，实际上就体现了我们与计算机的交流过程。比如，您可以通过使用鼠标点击“开始”，选择关机来命令计算机执行关闭计算机的行动；您也可以通过只是简单地双击一下桌面上的《星际争霸》快捷方式来让计算机运行《星际争霸》这款经典的即时战略游戏。也许您认为这些命令就如同吃饭睡觉一样简单，这是因为图形化界面已经为我们把复杂难以理解的命令操作封装成人人都能非常容易上手的操作方式，使得我们只需要点点鼠标或是敲几下键盘就能轻而易举地完成。</span><br><span class="line"></span><br><span class="line">而DOS命令，对于不少新接触计算机的朋友来说可能是一个高深而神秘的词。不过，如果您多少还懂点英语的话，这些问题可能就变得是很容易被理解的。假如我们打算把电脑D盘里的一个叫做<span class="string">&quot;a.txt&quot;</span>的文本文件复制到E盘里，您也许能够提出使用Ctrl+C复制该文件，然后再在E盘中Ctrl+V将其粘贴到此处；或是建议按住Ctrl键然后直接拖动到E盘，又或是用右键拖动后再选择，等等。这些都是图形界面里的操作，如果我们想用DOS命令来实现这一操作该如何去做呢？copy D:\a.txt E:\a.txt 这句英文看起来更像是在尝试与电脑进行聊天。不过您必须得保证您的发言有着严格正确的格式，电脑才会按照您的意图去执行。以下为常用文件操作DOS命令：</span><br><span class="line"></span><br><span class="line">dir 　　　列文件名</span><br><span class="line"><span class="built_in">cd</span>　　　　改变当前目录</span><br><span class="line">ren 　　　改变文件名</span><br><span class="line">copy　　　拷贝文件</span><br><span class="line">del 　　　删除文件</span><br><span class="line">md　　　　建立子目录</span><br><span class="line">rd　　　　删除目录</span><br><span class="line">deltree　 删除目录树</span><br><span class="line">format　　格式化磁盘</span><br><span class="line">edit　　　文本编辑</span><br><span class="line"><span class="built_in">type</span>　　　显示文件内容</span><br><span class="line">mem 　　　查看内存状况</span><br><span class="line">以下是新增加的命令</span><br><span class="line"><span class="built_in">help</span>　　　显示帮助提示</span><br><span class="line">cls 　　　清屏</span><br><span class="line">move　　　移动文件，改目录名</span><br><span class="line">more　　　分屏显示</span><br><span class="line">xcopy 　　拷贝目录和文件</span><br><span class="line">[2]</span><br><span class="line"></span><br><span class="line">仅仅是告诉您有哪些命令可以使用，此时您仍不会明白DOS命令的操作用法。我们不妨从例子中学习并理解。</span><br><span class="line"></span><br><span class="line">可以在 开始-&gt;所有程序-&gt;附件-&gt;命令提示符 找到并打开MS-DOS命令提示符。或是按WIN键(键盘上有微软标示的那个按键)再按R键，然后输入 cmd 并按回车，也能打开命令提示符。无论您对这个黑色而单调的界面感到是多么的新奇、或是陌生、甚至是厌恶，此时您都不会再计较了，重要的是您得尝试一下所谓的DOS命令。如果您正在用Windows XP操作系统(XP自带的DOS5使用方便，便于理解，以下均以XP操作系统为例进行说明)，您将可能看到有类似 C:\Documents and Settings\Administrator&gt; 的这么一行文字。这说明，您此时的工作位置就在 C:\Documents and Settings\Administrator 里。当然 Administrator 也可能是别的词，这取决于您当前登录的用户名以及该用户所设定文件夹的名称。而使用 Windows9X 操作系统的命令提示符将会看到经典的 C:\WIDNOWS&gt; 。</span><br><span class="line"></span><br><span class="line">此时我们来尝试一下使用一些基本的DOS命令，</span><br><span class="line">比如，键入 dir (不区分大小写)并按回车后，我们会得到当前文件夹下所有的子文件夹和文件的相关信息。输入 dir c:\windows ，可以查看指定的路径文件夹(这里假定我们指定的文件夹是C盘的WINDOWS文件夹)中的信息。在输入该命令后，只见刷地一下啥也没看清，N多文件或文件夹就已一闪而过，因为一个屏幕无法显示这么多的文件或文件夹。好在在DOS5中我们可以用鼠标滚轮向前滚动查看更多的内容。然而，早期的DOS版本并不具有保存多页信息的功能，我们不妨使用 dir 的一些参数(适当的参数加在相应的命令后面可以实现更多的扩展功能)，比如 /p ，即输入 dir c:\windows /p 。这样就能在每显示一个屏幕的内容后暂停一下以便查看。</span><br><span class="line"></span><br><span class="line">关于 dir 命令的用法和参数还有更多。除了 dir 以外，每一种命令的用法和参数都不少。看到这里您也许会觉得很沮丧，认为自己没有天赋、也没有工夫来死记硬背这些该死的命令。事实上我也有同感。命令的具体用法在帮助里、在网上都能轻而易举地找到。只要输入 dir /? 就能得到关于dir命令完整的使用介绍，而且是中文的。同理，任何命令后面跟上 /? 的参数，都能得到该命令的完整说明。当然，也可以在网上搜索<span class="string">&quot;DOS命令&quot;</span>等关键字来查找具体的DOS用法(推荐一个洪恩在线)。我们所需要的只是知道执行什么操作时用什么命令就行了，即使是连命令的名称都记不得了也没关系，都是现学现用的嘛。</span><br><span class="line"></span><br><span class="line">很多情况下，我们只需要记住一条命令 <span class="built_in">help</span> ，就能掌握整个DOS命令。比如直接输入 <span class="built_in">help</span> 可以得到命令的帮助信息。输入 <span class="built_in">help</span> dir 就能得到命令 dir 的具体参数及其用法(等同与 dir /? )。不仅仅是DOS命令，很多带有命令提示的工具都有<span class="built_in">help</span>提供帮助和提示，例如MATLAD中的命令行，又如BattleNet中的以斜杠 / 开头的命令等等。</span><br><span class="line"></span><br><span class="line">下面以一段例子来介绍DOS命令的具体用法(灰色背景的文字为DOS命令，可以尝试在命令提示符中输入测试查看效果)。</span><br><span class="line"></span><br><span class="line">d:</span><br><span class="line">转到驱动器D盘下，此时我们能看到 D:\&gt; 的提示符(前提是您的电脑硬盘必须至少划分出D盘)。如果看到的不是就再输入下一行命令。</span><br><span class="line"><span class="built_in">cd</span>\</span><br><span class="line">这一条命令 <span class="built_in">cd</span> 的作用是改变当前的工作目录，后面加上 \ 表示返回到该驱动器的最顶级目录。另外，一个句点 . 表示当前文件夹，两个连续的句点 .. 则表示上一级文件夹， <span class="built_in">cd</span>.. 即目录向上一级。</span><br><span class="line">md <span class="built_in">test</span></span><br><span class="line">正如前文所说，命令 md 的作用是创建子目录，此时打开D盘看一看，是不是多了一个名叫<span class="built_in">test</span>的文件夹(如果不是事先早已存在的话)。事实上该命令的完整写法为 md d:\<span class="built_in">test</span> ，由于当前目录已经在D盘了，所以我们把具体的路径给简化了。</span><br><span class="line">md <span class="string">&quot;test my folder&quot;</span></span><br><span class="line">同理，在D盘创建一个名为<span class="built_in">test</span> my folder的文件夹。加双引号的目的是告诉计算机我们要创建一个名字中含有空格的文件夹，而不是分别创建名字分别为<span class="built_in">test</span>、my和folder的3个不同的文件夹。注：除了空格以外，在路径或文件名中含有 &amp;()[]&#123;&#125;^=;!<span class="string">&#x27;+,`~ 特殊字符时也需要用双引号引用起来，以便机器能够正确地识别。</span></span><br><span class="line"><span class="string">cd test</span></span><br><span class="line"><span class="string">改变当前工作目录，此时不再是D盘了，而是在 D:\test 的目录下。</span></span><br><span class="line"><span class="string">echo Hello world&gt;a.txt</span></span><br><span class="line"><span class="string">遇到了一个新命令 echo ，它可以将某某内容显示出来。只考虑 echo Hello world 就是把字符串 Hello world 显示出来而已。后面使用了符号 &gt; 表示将这句 Hello world 写入到某文件中。结果为：在 D:\test 文件夹里多了一个叫 a.txt 的文件，其内容为 Hello world 。</span></span><br><span class="line"><span class="string">copy a.txt &quot;d:\test my folder&quot;</span></span><br><span class="line"><span class="string">这就是将当前文件夹里的那个 a.txt 文件复制到 D:\test my folder 文件夹中</span></span><br><span class="line"><span class="string">copy a.txt &quot;..\test my folder\b.txt&quot;</span></span><br><span class="line"><span class="string">还是复制 a.txt ，但这次的路径与上一条命令的写法不同。 .. 表示先向上一级，再挪到 test my folder 文件夹里。其实还是复制到 D:\test my folder 这个文件夹里了。这次复制过去的文件名也不一样，因为这次我们指定了要复制过去的目标文件名为 b.txt 。</span></span><br><span class="line"><span class="string">copy a.txt &quot;..\test my folder\c.bmp&quot;</span></span><br><span class="line"><span class="string">再次复制 a.txt 到同样的文件夹里。不过这次不光改文件的标题名了，连文件的后缀名也改了。打开 D:\test my folder 文件夹检查一下，是不是多了名字分别叫 a.txt b.txt c.bmp 的3个文件。</span></span><br><span class="line"><span class="string">cd..</span></span><br><span class="line"><span class="string">向上一级</span></span><br><span class="line"><span class="string">cd &quot;test my folder&quot;</span></span><br><span class="line"><span class="string">进入 D:\test my folder 文件夹</span></span><br><span class="line"><span class="string">ren c.bmp d.bin</span></span><br><span class="line"><span class="string">重新命名文件 c.bmp 为 d.bin 。完整地写法为 ren &quot;d:\test my folder\c.bmp&quot; d.bin 。</span></span><br><span class="line"><span class="string">del *.txt</span></span><br><span class="line"><span class="string">删除当前工作目录中所有文件后缀名为 .txt 的文件。 * 表示通配符。例如：a.* 表示所有文件标题为 a 的文件，不论后缀名。 *.* 则表示任何标题名和任何后缀名的文件，即所有文件。此时结果应为：该目录里的 a.txt b.txt 这两个文件已经不存在了，即使是在回收站里也找不到了。</span></span><br><span class="line"><span class="string">cd..</span></span><br><span class="line"><span class="string">向上一级</span></span><br><span class="line"><span class="string">rd test &quot;test my folder&quot;</span></span><br><span class="line"><span class="string">这一条命令是想同时移除 test 和 test my folder 这两个文件夹。不过结果并没有成功，因为它会提示：目录不是空的。test 文件夹中有 a.txt ，test my folder 文件夹中还有 d.bin ，因此文件夹删不掉。</span></span><br><span class="line"><span class="string">rd test &quot;test my folder&quot; /s</span></span><br><span class="line"><span class="string">如果加上 /s 这个参数后就能删除这两个文件夹以及其文件夹中所包含的所有内容了。注意：删除前请确认这些文件夹是否是您以前就有的文件夹，或者说里面是否存有任何有用文件，不要因为这个测试而丢掉了重要文件。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">1.3 我们的第一个批处理</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">在阅读本文之前您可能还对此一无所知，而现在您却已经略知一二了。如果您还无法理解DOS命令的工作原理或方式，在继续阅读下文之前，强烈建议具体操作实践一下。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">不过说了那么多，看起来似乎还没进入正题，您可能会不耐烦地说：目前批处理连个影子都没见到呢。如果我们只是简单地将上述例子中的命令集合起来，那么这就形成了一个批处理。做法是：先打开记事本，把下面这些您已经了如指掌(或者说只是略知一二)的命令复制进去。然后保存到某处(比如桌面)，并命名为 MyFirstBatch.bat (文件标题可自拟，但后缀名必须是.bat)。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">::::::::::::::::::::::::::::::::</span></span><br><span class="line"><span class="string">md &quot;d:\test&quot; &quot;d:\test my folder&quot;</span></span><br><span class="line"><span class="string">echo Hello world&gt;&quot;d:\test\a.txt&quot;</span></span><br><span class="line"><span class="string">copy &quot;d:\test\a.txt&quot; &quot;d:\test my folder&quot;</span></span><br><span class="line"><span class="string">copy &quot;d:\test\a.txt&quot; &quot;d:\test my folder\b.txt&quot;</span></span><br><span class="line"><span class="string">copy &quot;d:\test\a.txt&quot; &quot;d:\test my folder\c.bmp&quot;</span></span><br><span class="line"><span class="string">ren &quot;d:\test my folder\c.bmp&quot; d.bin</span></span><br><span class="line"><span class="string">::::::::::::::::::::::::::::::::</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">看到了吧！惊喜吧！疯狂吧！一个自己写的，或者说至少是自己已经能够完全理解的批处理文件 MyFirstBatch.bat 就这样诞生了。或许此时您对这东西能否正常工作还持有怀疑态度，但是在看到对该批处理文件双击运行完的结果以后，先前或多或少的怀疑也就荡然无存了……</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">双击后，显示的只是一闪就关闭了。正确的结果是在D盘多了名字分别为 test 和 test my folder 的两个文件夹。文件夹 test 里有一个叫 a.txt 的文件。而文件夹 test my folder 里有名字分别为 a.txt b.txt 和 d.bin 的3个文件。</span></span><br></pre></td></tr></table></figure>

<h2><span id="第二章显示篇">第二章：显示篇</span></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">有了前文作铺垫和基础，后面的便容易理解多了。这里主要讨论的是批处理过程中与显示相关的命令用法。先看一下 <span class="built_in">echo</span>、@、pause、&gt;、&gt;&gt;、title 、rem 这几个命令或符号的用法。</span><br><span class="line"></span><br><span class="line">2.1 <span class="built_in">echo</span> @ 和 pause</span><br><span class="line"></span><br><span class="line">在DOS命令提示符中使用 <span class="built_in">echo</span> /? 可以获得对 <span class="built_in">echo</span> 用法的解释。<span class="built_in">echo</span> on 用于打开命令的回显；<span class="built_in">echo</span> off 用于关闭命令的回显(默认情况下，ehco 是处于打开状态的)。只输入 <span class="built_in">echo</span> 可以获得当前的回显状态(是否处于打开状态)。输入 <span class="built_in">echo</span> 再加一段文字，例如 <span class="built_in">echo</span> Hello world! 可以显示出 Hello world! 这句信息。</span><br><span class="line"></span><br><span class="line">@ ，如果在某一条命令最前面加上 @ ，那么这一行命令就不会显示出来。与 <span class="built_in">echo</span> off 有着相似之处。 <span class="built_in">echo</span> off 以后的所有命令本身都不再显示出来；而 @ 只是将当前那一行的命令不显示出来。然而，至于命令所产生的输出结果，仍然会显示出来。这看起来似乎有些拗口，但我们会通过例子来很容易地理解它们。</span><br><span class="line"></span><br><span class="line">pause ，从字面上看就是暂停的意思，效果等同于将程序挂起，在按下任意键后才继续。</span><br><span class="line"></span><br><span class="line">::::::::测试显示状态.bat::::::::</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 例句一 此时回显为打开状态，因此前一句显示了命令行</span><br><span class="line">@<span class="built_in">echo</span> 例句二 此时回显虽然为打开状态，但命令前使用了@，因此未显示命令行本身</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span> 例句三 此时回显为关闭状态，因此未显示命令行本身</span><br><span class="line">@<span class="built_in">echo</span> 例句四 此时回显为关闭状态且使用了@，因此未显示命令行本身</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">上面的这一段批处理测试，有效地展示了在使用 <span class="built_in">echo</span> on 和 <span class="built_in">echo</span> off ，以及在命令前加上 @ 符号后，命令行本身的显示效果。</span><br><span class="line"></span><br><span class="line">2.2 &gt; 和 &gt;&gt;</span><br><span class="line"></span><br><span class="line">&gt; 表示将输出结果打印到某处。比如：<span class="built_in">echo</span> Hello world!&gt;d:\a.txt 表示将 Hello world! 这句话写入到 D:\a.txt 文件中。如果以前该文件中已经存在，并且有自己的内容，那么以前的内容就被覆盖掉了。比如我们再输入：<span class="built_in">echo</span> yo, whats up&gt;d:\a.txt ，那么文件 a.txt 中以前的 Hello world! 就变成了现在的新例句。</span><br><span class="line"></span><br><span class="line">&gt;&gt; 与 &gt; 类似，也可以将输出结果打印到某处。比如我们用 <span class="built_in">echo</span> nothin much, and u?&gt;&gt;d:\a.txt 将例句写到 a.txt 里时，该例句并不会覆盖原有的 yo, whats up 这句话，而是加在了原句的后面。</span><br><span class="line"></span><br><span class="line">如果一条命令后面跟上 &gt;nul ，比如 pause&gt;nul 表示将 pause 这条命令的输出显示到空设备里， nul 表示为空。用了 pause&gt;nul 这条命令后，<span class="string">&quot;按任意键继续...&quot;</span>的提示就不再出现了。</span><br><span class="line"></span><br><span class="line">对比 <span class="built_in">echo</span> off、@ 和 &gt;nul 。<span class="built_in">echo</span> off 表示这以后的所有命令的本身不再显示了，直到后面有 <span class="built_in">echo</span> on 的出现。而加在命令行前面的 @ 只是让当前这一行命令不显示。加在命令行后面的 &gt;nul 却可以让该命令的输出不显示。</span><br><span class="line"></span><br><span class="line">2.3 title 和 rem</span><br><span class="line"></span><br><span class="line">title 后面跟字符串可以改变当前命令提示符的标题名称。输入 title 这是新标题 后，该命令提示符左上角的标题名称已经变为<span class="string">&quot;这是新标题&quot;</span>了。输入中文可以通过 Ctrl+空格 切换出中文输入法；也可以通过复制粘贴的方式输入。</span><br><span class="line"></span><br><span class="line">rem 的用法就很简单了，rem 后面跟上一段文字，在批处理中可以作为注释用。rem 和它后面跟的文字在实际运行时并不会起任何作用，只是为了方便人们阅读该批处理时更容易理解而已(如果您用过C的话，一定会联想到C语言里的 // 或 /* */ 的用法)。除了 rem 外，两个连续的冒号 :: 也起同样的作用。提示：rem 与 :: 的区别在于，rem 也是一种命令，在 <span class="built_in">echo</span> on 的情况下会被显示出来，而 :: 却不会。</span><br><span class="line"></span><br><span class="line">:::::::测试标题和注释.bat:::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">rem 上条命令表示以后所有的命令行不再显示自身，@表示连<span class="built_in">echo</span> off这一句都不显示，当前这一行只是注释而已，不参与程序的运行。</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 欢迎！</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line">title 现在标题已经换成这句了</span><br><span class="line"><span class="built_in">echo</span> 标题已更改</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 现在使用了暂停，按任意键后该批处理结束~</span><br><span class="line">pause&gt;nul</span><br><span class="line">rem 不显示pause的输出提示，而是使用我们自己定义的暂停提示。</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">2.4 其他命令</span><br><span class="line"></span><br><span class="line">prompt ，这就是命令提示符中所谓的<span class="string">&quot;提示符&quot;</span>了。在命令提示符中输入 prompt 加一段文字能够使得提示符不再是以传统的路径名和大于号组成的，而是以我们刚才输入的那段文字开头的。这也许不是很好理解，或者您对 prompt 的含义还不清楚或只知道其字面含义。这并不要紧，如果您只要简单地输入 prompt 提示符 就能很快地明白 prompt 的含义了。此外，要想恢复以前的路径名和大于号为开头的提示符，只需要再输入 prompt <span class="variable">$p</span><span class="variable">$g</span> 即可。这里<span class="variable">$p</span> 表示当前驱动器和路径， <span class="variable">$g</span> 表示大于号。因为一些特殊的格式或符号需要用 $ 加特定的字母来表示。具体的说明可以用 <span class="built_in">help</span> prompt 来查询。</span><br></pre></td></tr></table></figure>

<h2><span id="第三章赋值-调用-参数">第三章：赋值 调用 参数</span></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br></pre></td><td class="code"><pre><span class="line">3.1 赋值</span><br><span class="line"></span><br><span class="line">3.1.1 给变量赋予一个文字字符串的值</span><br><span class="line"></span><br><span class="line">说到赋值，就得先弄懂 <span class="built_in">set</span> 这条命令。<span class="built_in">set</span> 这条命令比较复杂，在命令提示符中键入 <span class="built_in">set</span> /? 后得到的帮助信息也很多。不过，简单地说，使用 <span class="built_in">set</span> 跟上变量，再用等号 = 跟上字符串就能简单地给该变量赋值了。例如 <span class="built_in">set</span> var=Hello world! 。为了确认一下变量 var 的值是否是 Hello world! ，可以用 <span class="built_in">set</span> var 来查看变量 var 的值。用 <span class="built_in">set</span> v 可以查看所有以字母 v 开头变量的值。直接输入 <span class="built_in">set</span> 可以查看所有变量的值。另外，变量两侧加上百分符号 % 用来表示该变量的值(内容)。这样做可以将该变量的值赋给其他变量或是用做计算显示等处理。</span><br><span class="line"></span><br><span class="line">3.1.2 给变量赋予一个数值型的值</span><br><span class="line"></span><br><span class="line">在 <span class="built_in">set</span> 后面加上 /a 的参数可以给变量赋予一个数值型的值，例如 <span class="built_in">set</span> /a var=48 表示将数字48赋给变量var。该数值型的变量是一个32位的整数型数值，即占用4个字节，能表示的数值个数为2的32次方，含正负号，范围为：-2147483648~2147483647。</span><br><span class="line"></span><br><span class="line">3.1.3 从外部获得输入的赋值方式</span><br><span class="line"></span><br><span class="line">在 <span class="built_in">set</span> 后面加上 /p 的参数，可以将变量设成用户输入的一行输入。读取输入行之前，显示指定的 提示文字。当然，提示文字也可以是空的。比如 <span class="built_in">set</span> /p var=请输入一些文字： ，可以显示出一段提示文字<span class="string">&quot;请输入一些文字：&quot;</span>并能将用户输入的信息存到变量var里。/p 的参数还有很多诸如对字符串的替代、提取、增减等功能，具体可以参考 <span class="built_in">set</span> 的相关帮助信息。</span><br><span class="line"></span><br><span class="line">3.1.4 变量的值的赋予、显示、变换、计算等功能</span><br><span class="line"></span><br><span class="line">可能此时有些朋友对百分号 % 的理解还处于迷茫状态，对此我们可以做一些实验。首先，就像前文所说的那样，给一个叫做 var 的变量赋值Hello world! (在命令行里输入 <span class="built_in">set</span> var=Hello world! )。然后我们的打算把变量 var 里的值赋给另一个变量 var1 ，做法是：<span class="built_in">set</span> var1=%var% ，此时 var1 里的值也是 Hello world! 了。假如不使用百分号 % 仅仅是 <span class="built_in">set</span> var1=var 的话，那么此时变量 var1 所得到的值仅仅是 var 这3个字母而已。再回顾一下 <span class="built_in">echo</span> 的用法，分别尝试输入 <span class="built_in">echo</span> var1 和 <span class="built_in">echo</span> %var1% ，所得到的返回输出分别为：var1 和 Hello world! 。</span><br><span class="line"></span><br><span class="line">此外，百分号可以对变量中的字符串有效地进行编辑或变换。</span><br><span class="line">值得一提的是替换功能，其格式为：原始变量的名称后面跟上冒号 : ，再加上想要被代替的内容，紧接着一个等号 = ，然后再加上用来代替的新内容，最后用两个百分号把以上这些包括起来即可。虽然此时原始变量的值并没有改变，但百分号里的内容可以赋给一个变量，这个变量可以是原始变量。例：<span class="built_in">echo</span> %var:o=z% ，效果为把 Hello world! 里所有的字母 o 都用字母 z 代替，并显示出来，然而变量 var 的值却没有变化。</span><br><span class="line">当然，我们并不会满足于仅仅是代替一个字母——有时候我们需要代替两个。<span class="built_in">set</span> var2=%var:ld=ms and bugs% ，这条命令可以在把 Hello world! 里的 ld 替换成 ms and bugs 并将新的结果赋给变量 var2 ，变量 var 仍然不会变化。输入 <span class="built_in">echo</span> %var2% 确认一下结果是否为我们所期待的 Hello worms and bugs! 。</span><br><span class="line"></span><br><span class="line">对变量中的字符串在特定位置上的删减将用到这样的格式：%var:~m% 和 %var:~m,n% 。m 和 n 为整数参数。数字 m 为正数表示取变量 var 中从左侧数第 m 个字符(单字节字符)以后的内容；m 为负数则表示取变量 var 从右侧数第 -m 个字符以及其右侧的所有的字符，这就是第一条命令所产生的新字符串。如果数字 n 为正数，表示在上述新字符串中，从其左侧取 n 个字符的内容；若 n 为负数，则从其左侧取字符直到还剩下 -n 个字符为止的内容。</span><br><span class="line"></span><br><span class="line">如果您坚持认为这种抽象的表达方式是根本无法解释清楚这该死的 m 和无耻的 n 究竟是怎么回事的话，不如实验一下下面的例子。为了方便查看效果，我们假定变量 var 中的内容为 1234567890 (<span class="built_in">set</span> var=1234567890)，然后依次输入以下命令并查看相应的结果。</span><br><span class="line">输入的命令　　　　结果　　　　效果　　　　　　　　　　　　　　　　　　　</span><br><span class="line"><span class="built_in">echo</span> %var%　　　　1234567890　显示所有　　　　　　　　　　　　　　　　　</span><br><span class="line"><span class="built_in">echo</span> %var:~4%　　 567890　　　从第4个字符以后开始显示　　　　　　　　　</span><br><span class="line"><span class="built_in">echo</span> %var:~4,3%　 567　　　　 从第4个字符以后开始显示，并只显示前3个　　</span><br><span class="line"><span class="built_in">echo</span> %var:~-4%　　7890　　　　从倒数第4个字符开始显示　　　　　　　　　</span><br><span class="line"><span class="built_in">echo</span> %var:~-4,3%　789　　　　 从倒数第4个字符开始显示，并只显示前3个　　</span><br><span class="line"><span class="built_in">echo</span> %var:~4,-2%　5678　　　　从第4个字符以后开始显示，显示到还剩2个为止</span><br><span class="line"><span class="built_in">echo</span> %var:~0,3%　 123　　　　 从头开始显示，并只显示前3个字符　　　　　</span><br><span class="line"><span class="built_in">echo</span> %var:~0,-3%　1234567　　 从头开始显示，显示到还剩3个字符为止　　　</span><br><span class="line"></span><br><span class="line">此外，<span class="built_in">set</span> 也可以对数值型的变量进行常见的运算操作。</span><br><span class="line"><span class="built_in">set</span> /a num=48</span><br><span class="line"><span class="built_in">set</span> /a result=%num%+12</span><br><span class="line"><span class="built_in">echo</span> %result%</span><br><span class="line">上面的命令表示先给数值 48 赋给变量 num ，然后再把变量 num 的数值与数值 12 相加后的结果赋给变量 result 。第3行可以显示变量 result 的值，结果很明显，是 60 。</span><br><span class="line"></span><br><span class="line">3.2 调用</span><br><span class="line"></span><br><span class="line">与很多编程类的东西一样，批处理并不一定非得按照文本中命令的排列顺序一行一行地执行。如果遇到了 goto、call、start 这样的跳转、调用、启动等语句，程序通常会变得多层化，执行起来会更加有效。</span><br><span class="line"></span><br><span class="line">3.2.1 跳转</span><br><span class="line"></span><br><span class="line">goto 对于多少有点编程基础的朋友来说，想必不是一件难以理解的东西。goto 跟上标签就能直接让程序从该标签处开始继续执行随后的命令，不论标签的位置是在该 goto 命令的前面还是后面。标签必须以单个冒号 : 开头，但不区分大小写。有个特殊的标签 :EOF 或 :eof 能将控制转移到当前批脚本文件的结尾处，它是不需要事先定义的。</span><br><span class="line">::::::::::::跳转.bat::::::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">goto :FirstLable</span><br><span class="line"></span><br><span class="line">:SecondLable</span><br><span class="line"><span class="built_in">echo</span> 然后显示这句</span><br><span class="line">pause</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:FirstLable</span><br><span class="line"><span class="built_in">echo</span> 首先显示这句</span><br><span class="line">pause</span><br><span class="line">goto :SecondLable</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">3.2.2 调用</span><br><span class="line"></span><br><span class="line">call 主要体现在两个方面：一是调用该批处理以外的另一个批处理(事实上调用该批处理本身也可以，只是可能会带来不必要的死循环)；另一方面是有着与 goto 类似的向特定标签处跳转的功能。然而，call 的独特之处在于：在调用的批处理或标签后的内容处理完成以后，控制会继续执行 call 后面的语句。我们先来看一下用 call 进行跳转的效果。为了方便对比，我们将上面的批处理作如下修改(浅色文字为修改部分)。</span><br><span class="line">::::::::::::跳转.bat::::::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">call :FirstLable</span><br><span class="line"></span><br><span class="line">:SecondLable</span><br><span class="line"><span class="built_in">echo</span> 然后显示这句</span><br><span class="line">pause</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:FirstLable</span><br><span class="line"><span class="built_in">echo</span> 首先显示这句</span><br><span class="line">pause</span><br><span class="line">::goto :SecondLable</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line">在用 call 跳转到 :FirstLabel 处执行到程序结尾后(此时 call 的任务才刚刚完成)，会继续回到 call 语句后的 :SecondLabel 处。假如 goto :SecondLabel 这一句没有被注释掉的话，那么控制会跳转到 :SecondLabel 处直到 goto :EOF 处 call 的使命才真正完成。而且，call 在完成任务后，下面的 :SecondLabel 处内容会再次执行一遍。</span><br><span class="line"></span><br><span class="line">当 call 作为调用其他新的批处理的用途时，当前批处理就会暂停，直到新的批处理结束后，之前的批处理才会继续执行。例如：直接调用当前路径里的一个批处理 call test.bat ，或是要调用的批处理在当前路径向上一级的abc文件夹里 call ..\abc\test.bat ，也可以使用绝对路径找到目标批处理 call D:\abc\test.bat (路径的写法请参阅[前言]里命令 <span class="built_in">cd</span> 的用法介绍)。当然，一个最直观的例子往往更能说明问题。建立两个批处理(假设他们在同一路径下)<span class="string">&quot;调用.bat&quot;</span>和<span class="string">&quot;被调用.bat&quot;</span>，它们的内容分别为：</span><br><span class="line">::::::::::::调用.bat::::::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">echo</span> 这里是 调用.bat</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line">call 被调用.bat</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 现在又回到了 调用.bat</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">:::::::::::被调用.bat:::::::::::</span><br><span class="line"><span class="built_in">echo</span> 这里是 被调用.bat</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line">编辑并保存好这两个批处理，然后运行批处理<span class="string">&quot;调用.bat&quot;</span>，所得到的结果跟我们预期的完全一致。</span><br><span class="line"></span><br><span class="line">3.2.3 启动</span><br><span class="line"></span><br><span class="line">start 虽然也不是一个简单的命令，但用法绝对不难理解。来几个例子：start msconfig 用来打开<span class="string">&quot;系统配置应用程序&quot;</span>；start notepad 则可以打开一个记事本；start <span class="string">&quot;这就是所谓的标题&quot;</span> cmd 用来打开一个新的命令提示符；start <span class="string">&quot;随便写个标题&quot;</span> http://www.baidu.com 便打开百度的首页；而 start <span class="string">&quot;开玩了&quot;</span> E:\game\starcraft\starcraft.exe 却是开始星际争霸(如果您的电脑里安装了星际且路径与上述一致的话)等等。虽然 start 的参数很多(具体用法在输入 <span class="built_in">help</span> start 后可以得到)，但通常情况下我们只需要知道 start 后面加上标题，再跟上想要执行程序、命令或网址即可。值得注意的是：标题要用双引号引用起来，否则会被作为可执行的文件来处理；所要执行的东西如果不是系统内部程序或命令的话，则需要我们给出具体的路径，比如绝对路径。</span><br><span class="line">当然，start 也可以打开另外一个批处理。这看起来似乎与 call 相仿，却有一些区别。为了对比 call 和 start 之间的效果差异，可以在上一个例子中修改<span class="string">&quot;调用.bat&quot;</span>中的 call 被调用.bat 为 start 被调用.bat 。从<span class="string">&quot;调用.bat&quot;</span>开始执行后，<span class="string">&quot;被调用.bat&quot;</span>也的确能够被调用。但之前的批处理也同时继续执行着。事实上，此时这两个程序已经完全独立开了，是两个独立的进程。</span><br><span class="line"></span><br><span class="line">3.3 参数</span><br><span class="line"></span><br><span class="line">上文提到了调用，这里就不得不补充一下参数。何谓参数？假如您要使用命令 dir ，那后面所跟的路径文件名也好、/p (分页显示)也好、/w (宽列表显示)也好、又或者是 /s (所有目录及子目录)也好，这些都能被称作为参数。对于一条命令来说，它除了能返回(输出)给我们一些信息以外，我们还经常给它输入一些信息，被输入的信息就是参数；然而，对于一个批处理来说，它除了能为我们工作(输出结果)以外，有时也需要外界给他一些信息。</span><br><span class="line"></span><br><span class="line">3.3.1 参数的传递</span><br><span class="line"></span><br><span class="line">为了很好的认识到这一点，我们先稍微修改一下上文所使用的<span class="string">&quot;被调用.bat&quot;</span>这个批处理文件，使之能够接受处理外界给它的输入参数。</span><br><span class="line">:::::::::::被调用.bat:::::::::::</span><br><span class="line"><span class="built_in">echo</span> 这里是 被调用.bat</span><br><span class="line"><span class="built_in">echo</span> 您输入的第1条参数为 %1</span><br><span class="line"><span class="built_in">echo</span> 您输入的第2条参数为 %2</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line">其中，%1 和 %2 分别代表运行<span class="string">&quot;被调用.bat&quot;</span>批处理时所跟的两个参数。那么它该如何获得所谓的参数1和参数2呢。双击运行<span class="string">&quot;被调用.bat&quot;</span>当然是不行的了。可以在命令提示符里输入<span class="string">&quot;被调用.bat&quot;</span>的全名，并在后面加上两个参数即可(如果<span class="string">&quot;被调用.bat&quot;</span>不在当前工作路径，需要输入<span class="string">&quot;被调用.bat&quot;</span>的路径，比如绝对路径，以便让计算机找到它)。就像：D:\批处理\<span class="built_in">test</span>\被调用.bat Tom and Jerry 。运行时我们会发现 %1 和 %2 分别显示为 Tom 和 and ，Jerry 为作为第3个参数来处理，但该批处理中却未用到 %3 。提示：在XP等操作系统中，对于汉字的输入可用 Ctrl + 空格 切换出中文输入法；也可以按 Tab 键让其自动切换并补充完成您所想要输入的路径。</span><br><span class="line"></span><br><span class="line">为了进一步验证这一点，我们再改一下批处理<span class="string">&quot;调用.bat&quot;</span>，让它在调用其他批处理时加上参数</span><br><span class="line">::::::::::::调用.bat::::::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">echo</span> 这里是 调用.bat</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line">call 被调用.bat Hello world!</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 现在又回到了 调用.bat</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">3.3.2 参数的输入与输出</span><br><span class="line"></span><br><span class="line">参数不仅可以是值，也可以是变量。如果是变量的话，它既可以作为输入，也可以作为输出。为此，我们需要分别再改一次<span class="string">&quot;调用.bat&quot;</span>和<span class="string">&quot;被调用.bat&quot;</span>。</span><br><span class="line"></span><br><span class="line">::::::::::::调用.bat::::::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">echo</span> 这里是 调用.bat</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /p Num=请输入一个整数:</span><br><span class="line"><span class="built_in">set</span> Square=</span><br><span class="line"></span><br><span class="line">call 被调用.bat Num Square</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 现在又回到了 调用.bat ，而且，%Num% 的平方是 %Square% 。</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">:::::::::::被调用.bat:::::::::::</span><br><span class="line"><span class="built_in">echo</span> 这里是 被调用.bat</span><br><span class="line"><span class="built_in">echo</span> 您输入的第1条参数为 %1</span><br><span class="line"><span class="built_in">echo</span> 您输入的第2条参数为 %2</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a %2 = %1 * %1</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 经过计算后，您输入的第1条参数为 %1</span><br><span class="line"><span class="built_in">echo</span> 经过计算后，您输入的第2条参数为 %2</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">可以看出，在批处理<span class="string">&quot;被调用.bat&quot;</span>中的 call 被调用.bat Num Square 里，变量 Num 和 Square 被传递过去的只是变量名而已，而不是变量中的数值。另外，尝试将<span class="string">&quot;调用.bat&quot;</span>中的 call 被调用.bat Num Square 换为 call 被调用.bat %Num% Square ，然后再对比一下结果，相信您一定会有所收获。</span><br><span class="line"></span><br><span class="line">3.3.3 函数</span><br><span class="line"></span><br><span class="line">在一个批处理的内部调用时，使用参数会怎么样呢？那么这就是函数的雏形。把之前的<span class="string">&quot;跳转.bat&quot;</span>稍微也改一下</span><br><span class="line">::::::::::::跳转.bat::::::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">call :FirstLable 很好很强大</span><br><span class="line"></span><br><span class="line">:SecondLable</span><br><span class="line"><span class="built_in">echo</span> 然后显示这句</span><br><span class="line">pause</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:FirstLable</span><br><span class="line"><span class="built_in">echo</span> 首先显示这句，后面跟的参数为 %1</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">对于 goto 所跟的标签或 start 所跟的程序，它们后面能否加参数呢？答案是：前者是否定的；后者是肯定的，试试看。</span><br><span class="line"></span><br><span class="line">3.4 后记</span><br><span class="line"></span><br><span class="line">写了那么多天，也许我们应该来点更实际更有用的东西。前几天无意中发现了一个163邮箱登录器，在此作为例子介绍一下。</span><br><span class="line"></span><br><span class="line">:::::::163邮箱登录器.bat::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">::不显示命令行</span><br><span class="line">mode con cols=32 lines=8</span><br><span class="line">::设置窗口的尺寸</span><br><span class="line">title 163邮箱登录器</span><br><span class="line">::设置该批处理的标题</span><br><span class="line">color 3a</span><br><span class="line">::设置背景(暗靛色)和文字(亮绿色)的颜色。</span><br><span class="line">::另外，在命令提示符中输入 color/? 可获得详细的颜色信息</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /p username=帐号：</span><br><span class="line"><span class="built_in">set</span> /p password=密码：</span><br><span class="line">::给出输入的提示，并将字符串赋给相应的变量</span><br><span class="line"></span><br><span class="line">start <span class="string">&quot;正在登录邮箱&quot;</span> <span class="string">&quot;https://reg.163.com/logins.jsp?username=%username%&amp;password=%password%&amp;url=http://fm163.163.com/coremail/fcg/ntesdoor2&quot;</span></span><br><span class="line">::事实上，那个网址链接才是整个程序的核心部分，注意其中的 %username% 和 %password% 分别是指变量 username 和 password 里的字符串内容。</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br></pre></td></tr></table></figure>

<h2><span id="第四章条件-循环">第四章：条件 循环</span></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br></pre></td><td class="code"><pre><span class="line">4.1 条件 <span class="keyword">if</span></span><br><span class="line"></span><br><span class="line">4.1.1 <span class="keyword">if</span> 是一种极其普遍却又非常重要的语句，说得严重点这就是一种能够体现程序灵魂的东西之一。在大多数的编程语言(例如 C VB JScript Java 等)中都能看到 <span class="keyword">if</span> 的身影。<span class="keyword">if</span> 语句的功能正如它的字面含义一样——如果。批处理程序的语言格式相比较我们常见的 C 语言来说，并不是那么的严谨，至少看上去是更自由一些。比如 <span class="keyword">if</span> 在批处理中的具体用法及格式就有很多，使用和发挥的余地也很大，但随之带来的问题就是我们不得不多花一些时间来记忆其各种用法和格式并分辨它们之间的差异。为了简化该问题使之更容易理解，在此我们并不打算过早地接触 <span class="keyword">if</span> 的全面用法或格式，而是从最基本的用途开始。</span><br><span class="line"></span><br><span class="line">4.1.2 在有前几篇文章的学习作基础的情况下，如果您能理解以下几行语句，那么您已经对 <span class="keyword">if</span> 有了深刻的认识了(其中两个连续的等号 == 表示：是否等于)。</span><br><span class="line"><span class="built_in">set</span> var=Tom</span><br><span class="line"><span class="keyword">if</span> %var%==Tom <span class="built_in">echo</span> It works</span><br><span class="line"><span class="keyword">if</span> %var%==Jerry <span class="built_in">echo</span> We will never see this</span><br><span class="line">如果变量 var 的值为 Tom Hanks ，即中间含有空格之类的特殊符号，那么我们在使用 <span class="keyword">if</span> 时，就得为字符串加上双引号，就像 <span class="keyword">if</span> <span class="string">&quot;%var%&quot;</span>==<span class="string">&quot;Tom Hanks&quot;</span> <span class="built_in">echo</span> It works (注意，给字符串加上双引号后，在进行判断的时候会连双引号一起考虑进去。所以，为了使两边的对比均衡，所以一定要在 == 两边的两个字符串上同时都加双引号)。这里也体现了批处理程序语言格式的多样性(如果您熟悉 C 语言格式的话，就知道一串字符总是要被双引号引起来)。不过为了方便记忆，我们在使用 <span class="keyword">if</span> 的时候，不妨总是在字符串上使用双引号，这样既好阅读，又不容易引起歧异。</span><br><span class="line"></span><br><span class="line">::::::::::改变颜色.bat::::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">echo</span> 您希望字体的颜色是红色还是绿色？</span><br><span class="line"></span><br><span class="line">:RETRY</span><br><span class="line"><span class="built_in">set</span> /p choice=请输入您的选择，R 或者 G ：</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%choice%&quot;</span>==<span class="string">&quot;R&quot;</span> goto R</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%choice%&quot;</span>==<span class="string">&quot;r&quot;</span> goto R</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%choice%&quot;</span>==<span class="string">&quot;G&quot;</span> goto G</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%choice%&quot;</span>==<span class="string">&quot;g&quot;</span> goto G</span><br><span class="line">goto RETRY</span><br><span class="line"></span><br><span class="line">:R</span><br><span class="line">color c</span><br><span class="line"><span class="built_in">echo</span> 您选择了红色字体</span><br><span class="line">pause</span><br><span class="line"><span class="built_in">exit</span></span><br><span class="line"></span><br><span class="line">:G</span><br><span class="line">color a</span><br><span class="line"><span class="built_in">echo</span> 您选择了绿色字体</span><br><span class="line">pause</span><br><span class="line"><span class="built_in">exit</span></span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line">上面的例子能很好地说明了 <span class="keyword">if</span> 与 goto 的结合使用带来的实用效果。其中，在使用 <span class="keyword">if</span> 进行判断变量 choice 的值是否为字母 R 或 G 时，分别对其大小写都进行了判断。其实您还可以使用 <span class="keyword">if</span> 的参数 /i ，就像 <span class="keyword">if</span> /i <span class="string">&quot;%choice%&quot;</span>==<span class="string">&quot;r&quot;</span> ，这样在进行比较的时候就不区分大小写了。</span><br><span class="line"></span><br><span class="line">4.1.3 说到了 <span class="keyword">if</span> 就不得不说一下 <span class="keyword">else</span> ，<span class="keyword">else</span> 无法单独使用，必须与 <span class="keyword">if</span> 配合连用。</span><br><span class="line">:::::::::<span class="keyword">else</span>的用法.bat:::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%TIME:~0,2%&quot;</span> lss <span class="string">&quot;12&quot;</span> (</span><br><span class="line"><span class="built_in">echo</span> 现在是上午</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="built_in">echo</span> 现在是下午</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line">其中，变量 TIME 是动态环境变量之一，表示当前时间(在 <span class="built_in">set</span>/? 中有介绍)。%TIME:~0,2% 的含义还没忘记吧，意思是取变量 TIME 的前两个字符(忘记的朋友请参阅上一篇[赋值 调用 参数])。lss 是 <span class="keyword">if</span> 命令扩展用法，表示 小于 的意思。此外，还有等于、不等于、大于、大于等于、小于等于 的缩写，详细信息可以在 <span class="keyword">if</span>/? 中获得。因此，对于上述批处理的理解就是：如果当前时间(前两位表示小时)小于12(点)的话，那么将显示输出“现在是上午”，否则就显示为“现在是下午”。另外：这里的大小比较判断只是对其ASCII符的大小比较，并不是真正的数值型变量的比较，稍后下文会有关于数值型变量比较的介绍。</span><br><span class="line"></span><br><span class="line">对于 <span class="keyword">if</span> 和 <span class="keyword">else</span> 的编写格式有较严格的要求，尤其是在两个圆括号上，若以不正确的格式使用可能会导致 <span class="keyword">if</span> 或 <span class="keyword">else</span> 等命令无效。虽然上面的编写格式并不是唯一的，但使用统一、固定的格式编写代码会大大提高代码的可读性。为了加深对 <span class="keyword">if</span> <span class="keyword">else</span> 的理解，我们可以把上面的批处理扩展一下。</span><br><span class="line">:::::::::<span class="keyword">else</span>的用法.bat:::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%TIME:~0,2%&quot;</span> lss <span class="string">&quot;12&quot;</span> (</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%TIME:~0,2%&quot;</span> lss <span class="string">&quot; 6&quot;</span> (</span><br><span class="line"><span class="built_in">echo</span> 现在是凌晨</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="built_in">echo</span> 现在是上午</span><br><span class="line">)</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%TIME:~0,2%&quot;</span> lss <span class="string">&quot;18&quot;</span> (</span><br><span class="line"><span class="built_in">echo</span> 现在是下午</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="built_in">echo</span> 现在是晚上</span><br><span class="line">)</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">4.1.4 刚才提到的数值型变量的比较，其实很简单，就如下面的例子中描述的一样。</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">set</span> /a num=5</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> %num% == 5 (</span><br><span class="line"><span class="built_in">echo</span> 变量 num 等于 5</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> not %num% == 4 (</span><br><span class="line"><span class="built_in">echo</span> 变量 num 不等于 4</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a num = ( %num% + 3 ) * 2</span><br><span class="line">:: 变量 num 加3并乘2后再赋给变量 num 自身</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> %num% == 16 (</span><br><span class="line"><span class="built_in">echo</span> 经过运算后，现在变量 num 等于16</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> not %num% == 16 (</span><br><span class="line"><span class="built_in">echo</span> 此时的变量 num 不会不等于 16 ，因此这一句不会显示了</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">4.1.5 延迟变量扩充。考虑到读取一行文本时所遇到的目前扩充的限制时，延迟变量扩充是很有用的，而不是在执行的时候。下面的例子可以很好的说明直接变量扩充与延迟变量扩充的区别。</span><br><span class="line">::::::::延迟变量扩充.bat::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">setlocal EnableDelayedExpansion</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a num=5</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> %num% == 5 (</span><br><span class="line"><span class="built_in">set</span> /a num*=3</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 在 <span class="keyword">if</span> 语句之前，变量 num 等于 %num%</span><br><span class="line"><span class="built_in">echo</span> 但变量 num 在经过运算后，且由于延迟变量扩充被启用，变量 num 等于 !num!</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 但最终变量 num 还是等于 %num%</span><br><span class="line"></span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"><span class="keyword">if</span> 条件下的两行 <span class="built_in">echo</span> 在输出变量值的时候用到的符号不一样，一个是用百分号 % 包括起来的，另一个用的却是惊叹号 ! 。虽然在显示 %num% 之前已经使变量 num 的数值乘了3倍，但是由于没有延迟变量的扩充，使得 %num% 的结果仍然是 5 。但用 !num! 显示出的值已经变为 15 了。注意到批处理中的 setlocal EnableDelayedExpansion (setlocal/? 查看相关信息)，这表示开启延迟变量扩充。此时的 !num! 才有意义。不然 !num! 将无法被识别，因为在默认情况下，延迟变量扩充是被停用的。</span><br><span class="line"></span><br><span class="line">4.1.6 此外，<span class="keyword">if</span> 还有其他的用法—— <span class="keyword">if</span> exist 和 <span class="keyword">if</span> defined 。<span class="keyword">if</span> exist 可判断文件是否存在，就像这样：</span><br><span class="line"><span class="keyword">if</span> exist <span class="string">&quot;D:\test my folder\a.txt&quot;</span> (</span><br><span class="line">del <span class="string">&quot;D:\test my folder\a.txt&quot;</span></span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="built_in">echo</span> 您所要删除的文件不存在</span><br><span class="line">)</span><br><span class="line">在对文件进行操作之前进行判断其是否存在很有意义，这使得代码更加健壮。</span><br><span class="line"></span><br><span class="line">而对于 <span class="keyword">if</span> defined 来说，与 <span class="keyword">if</span> exist 类似，只不过 <span class="keyword">if</span> defined 的判断对象不是文件，而是变量，它用于判断环境变量是否被定义。</span><br><span class="line"></span><br><span class="line">4.2 循环 <span class="keyword">for</span></span><br><span class="line"></span><br><span class="line">4.2.1 如果批处理不具备批量处理的功能，那么它就徒有虚名了。而命令 <span class="keyword">for</span> ，在某种意义上彻底体现出了批处理的强大快捷省事批量的作用。在看过 <span class="keyword">for</span>/? 后，可以归纳出 <span class="keyword">for</span> 大致可以分三种常用的类型(或者叫使用方法)。从针对的循环目标来看，它们分别是针对于文件、数字、以及文字。</span><br><span class="line"></span><br><span class="line">4.2.2 <span class="keyword">for</span> %i <span class="keyword">in</span> (*.*) <span class="keyword">do</span> @<span class="built_in">echo</span> %i 。这就是 <span class="keyword">for</span> 的一般使用格式。注意到其中的浅靛色文字 <span class="keyword">for</span> 、<span class="keyword">in</span> 和 <span class="keyword">do</span> ，是 <span class="keyword">for</span> 的固定用法。其内容可以理解为：在某一范围内(<span class="keyword">in</span>)，对于其中的某一文件来说(<span class="keyword">for</span>)，做如下的处理(<span class="keyword">do</span>)。而 <span class="keyword">for</span> %i <span class="keyword">in</span> (*.*) <span class="keyword">do</span> @<span class="built_in">echo</span> %i 就是在当前工作目录的所有文件中(<span class="keyword">in</span> (*.*))，对于其中的某一文件(<span class="keyword">for</span> %i)，做出显示其名称的处理(<span class="keyword">do</span> @<span class="built_in">echo</span> %i)。变量 i 仅在当前循环语句 <span class="keyword">for</span> 里起作用，%i 表示其值。</span><br><span class="line">注意：以上是直接在命令提示符里以命令的形式表达出来的写法；在批处理文件中应使用双百分号 %% 代替单百分比号 % ，就像：%%i。关于它们之间的区别我研究了好半天才分清楚 orz [具体请参阅后文第4.2.5节]。</span><br><span class="line"></span><br><span class="line">批量修改文件名是其比较有用的典例之一。看看下面的批处理</span><br><span class="line">:::::::批量修改文件名.bat:::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">setlocal EnableDelayedExpansion</span><br><span class="line"><span class="built_in">set</span> /a num=1</span><br><span class="line"><span class="keyword">for</span> %%i <span class="keyword">in</span> (D:\<span class="built_in">test</span>\*.txt) <span class="keyword">do</span> (</span><br><span class="line">ren <span class="string">&quot;%%i&quot;</span> !num!.txt</span><br><span class="line"><span class="built_in">set</span> /a num+=1</span><br><span class="line">)</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line">这个批处理并不难理解。就像第4.1.5节所说的：使用了 setlocal EnableDelayedExpansion 后，可以让 <span class="keyword">for</span> 或 <span class="keyword">if</span> 后面的执行语句中变量的值随其变化而不断更新(所以后面使用了 !num! 而不是 %num%)。整个批处理的处理过程就是对 D:\<span class="built_in">test</span>\*.txt 中的所有文本文件进行批量改名，文件名从 1.txt 开始依次为 2.txt 、3.txt ……。</span><br><span class="line">注意：请确保循环语句 <span class="keyword">in</span> 路径中的文件不是重要的文件，因为改名后将无法使用撤消，如果像我一样不小心把重要文件误改名的话就又要 orz 了一次。</span><br><span class="line"></span><br><span class="line">以上批处理是固定了文件的路径以及文件后缀名。为了增加该批处理的功能，我们可以让用户自己选择要进行改名的文件所在路径，以及选择所进行文件修改的后缀名。当然，有些朋友还希望有给文件批量加上前缀(比如：前缀1.txt 前缀2.txt 等等)。(关于 批量改文件名.bat 在第六章中还有进一步的修改)</span><br><span class="line">::::::::批量改文件名.bat::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">setlocal EnableDelayedExpansion</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /p zpath=请输入目标文件所在的路径：</span><br><span class="line"><span class="built_in">set</span> /p prefix=请输入文件名前缀(不能包含以下字符\/:*?<span class="string">&quot;&lt;&gt;|)：</span></span><br><span class="line"><span class="string">set /p ext=请输入文件的扩展名(例如 .txt)：</span></span><br><span class="line"><span class="string">set /a num=1</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">for %%i in (%zpath%\*%ext%) do (</span></span><br><span class="line"><span class="string">ren &quot;</span>%%i<span class="string">&quot; &quot;</span>%prefix%!num!.%ext%<span class="string">&quot;</span></span><br><span class="line"><span class="string">set /a num+=1</span></span><br><span class="line"><span class="string">)</span></span><br><span class="line"><span class="string">::::::::::::::::::::::::::::::::</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">4.2.3 也许大家注意到了，上面 for 的用法仅仅是针对多个文件来进行循环重复操作的。如果想对一系列有规律的数字进行循环，或是在一定的次数内对某个操作进行循环重复的执行，使用 for 也能够实现。/l 是可以跟在 for 后面的重要参数之一。比如：for /l %i in (5,3,16) do echo %i ，可以让数值型的变量 i 依次成为：5、8、11、14 。正如 in 里所描述的规律 (5,3,16) 一样，从 5 开始，每次增加 3 ，直到 16为止。同样，我们还可以试一下 for /l %i in (19,-4,3) do echo %i ，这次 i 是递减的规律。很明显，结果将依次显示为：19、15、11、7、3 。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">这样的用法很自然的能让我们想到，重复执行N遍完全一样的事情不再是麻烦而又无聊的了。在下面的例子里，您一定会找到惊喜的。</span></span><br><span class="line"><span class="string">::::::::::圆圈方阵.bat::::::::::</span></span><br><span class="line"><span class="string">@echo off</span></span><br><span class="line"><span class="string">setlocal EnableDelayedExpansion</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">set var=○</span></span><br><span class="line"><span class="string">for /l %%i in (1,1,7) do set var=%var%!var!</span></span><br><span class="line"><span class="string">:: 此时变量 var 已经变成一行连续的8个圆圈了</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">for /l %%i in (1,1,8) do (</span></span><br><span class="line"><span class="string">echo 这是第 %%i 份&gt;输出结果%%i.txt</span></span><br><span class="line"><span class="string">for /l %%j in (1,1,8) do echo %var%&gt;&gt;输出结果%%i.txt</span></span><br><span class="line"><span class="string">)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">echo 8 X 8 的 ○ 矩阵已经画好，并保存到8份文本文件里了</span></span><br><span class="line"><span class="string">pause</span></span><br><span class="line"><span class="string">::::::::::::::::::::::::::::::::</span></span><br><span class="line"><span class="string">注意：%%i ，上一节中提到过，在批处理文件中需要用连续的两个百分号 %% 来描述循环变量 i ，而不是一个。</span></span><br><span class="line"><span class="string">注意：%var% 与 !var! ，它们的用法与区别，在第4.1.5节中有解释。</span></span><br><span class="line"><span class="string">注意：i 与 j ，在循环里面再套循环时，前一个循环变量 i 在没有释放之前，不应该让第二个循环变量的名称与 i 重复。</span></span><br><span class="line"><span class="string">注意：&gt; 与 &gt;&gt; ，同样是向某设备里输出，但却有区别，请参阅第2.2节。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">4.2.4 for 也可以对指定范围内的文字进行循。for 后面跟参数 /f ，/f 后面跟选项，所指定的范围 in 里可以是一个文件里的文字，可以是一个字符串，也可以是一条命令的输出结果。我们首先以一个文件里的文字作为循环对象，循环时，每一行将被循环一次。</span></span><br><span class="line"><span class="string">::::::::::文字筛选.txt::::::::::</span></span><br><span class="line"><span class="string">@echo off</span></span><br><span class="line"><span class="string">echo 测试 文字筛选.txt 里每一行的首单词</span></span><br><span class="line"><span class="string">for /f %%i in (文字筛选.txt) do echo %%i</span></span><br><span class="line"><span class="string">pause</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">echo.</span></span><br><span class="line"><span class="string">echo skip=2 表示前两行被跳过</span></span><br><span class="line"><span class="string">for /f &quot;</span>skip=2<span class="string">&quot; %%i in (文字筛选.txt) do echo %%i</span></span><br><span class="line"><span class="string">pause</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">echo.</span></span><br><span class="line"><span class="string">echo tokens=2,4-6 表示提取每行的第2个、以及第4到6个单词</span></span><br><span class="line"><span class="string">for /f &quot;</span>skip=2 tokens=2,4-6<span class="string">&quot; %%i in (文字筛选.txt) do echo %%i, %%j, %%k, %%l.</span></span><br><span class="line"><span class="string">pause</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">echo.</span></span><br><span class="line"><span class="string">echo eol=N 表示当此行的首字母为 N 时，就忽略该行</span></span><br><span class="line"><span class="string">for /f &quot;</span>eol=N skip=2 tokens=2,4-6<span class="string">&quot; %%i in (文字筛选.txt) do echo %%i, %%j, %%k, %%l.</span></span><br><span class="line"><span class="string">pause</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">echo.</span></span><br><span class="line"><span class="string">echo delims=e 表示不再以空格区分每个词，而是以字母 e 作为间隔</span></span><br><span class="line"><span class="string">for /f &quot;</span>eol=N skip=2 tokens=2,4-6 delims=e<span class="string">&quot; %%i in (文字筛选.txt) do echo %%i, %%j, %%k, %%l.</span></span><br><span class="line"><span class="string">pause</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">echo.</span></span><br><span class="line"><span class="string">echo usebackq 表示双引号里的东西是文件名而不是字符串</span></span><br><span class="line"><span class="string">for /f &quot;</span>usebackq eol=N skip=2 tokens=2,4-6 delims=e<span class="string">&quot; %%i in (&quot;</span>文字筛选.txt<span class="string">&quot;) do echo %%i, %%j, %%k, %%l.</span></span><br><span class="line"><span class="string">pause</span></span><br><span class="line"><span class="string">::::::::::::::::::::::::::::::::</span></span><br><span class="line"><span class="string">作为测试，可以在上述批处理文件的同一路径下创建一个用于测试的文本文件 文字筛选.txt ，其内容为：</span></span><br><span class="line"><span class="string">Hello there!</span></span><br><span class="line"><span class="string">This text is an example of test for the batch file 文字筛选.bat.</span></span><br><span class="line"><span class="string">Notice the first letter in this line, N.</span></span><br><span class="line"><span class="string">If the eol charactor was set to be letter N.</span></span><br><span class="line"><span class="string">The third line will not be considered by the batch.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">4.2.5 ESCAPE字符 % ，通常被译为转义字符，但也有更形象的译名脱逸字符、逃逸字符等。也就是说 % 不仅仅将与其相关的特定字符串转义并替换为特定字符串，而且自身也会被“脱逸”。而且类似于C语言中的转义字符 \ ，双%会转义并脱逸为单百分号 % ，四%则脱为双百分号 %% 。[3]</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">注意下面这个批处理中的双百分号 %% 的用法</span></span><br><span class="line"><span class="string">::::::::::::::::::::::::::::::::</span></span><br><span class="line"><span class="string">@echo off</span></span><br><span class="line"><span class="string">set Text=Hello world!</span></span><br><span class="line"><span class="string">for /l %%i IN (0,1,11) do call echo %%Text:~%%i,1%%</span></span><br><span class="line"><span class="string">pause</span></span><br><span class="line"><span class="string">::::::::::::::::::::::::::::::::</span></span><br></pre></td></tr></table></figure>

<h2><span id="第五章组合命令-管道命令">第五章：组合命令 管道命令</span></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">5.1 组合命令</span><br><span class="line"></span><br><span class="line">组合命令 &amp; 、&amp;&amp; 和 || 是一类用于两个或多个命令语句之间起衔接作用的符号。这对于我们想一次性执行两条或多条命令，以及前面命令执行结果的成功与否作为后面命令是否被执行的衡量标准，起着决定性的作用。</span><br><span class="line"></span><br><span class="line">5.1.1 &amp;</span><br><span class="line"></span><br><span class="line">通过紧随的例子，<span class="built_in">echo</span> Checking what executable files we have <span class="keyword">in</span> WINDOWS... &amp; dir C:\WINDOWS\*.exe &amp; <span class="built_in">echo</span> And we got lots of stuff here. 我们不难理解 &amp; 在多个命令之间所起的连接作用。事实上，我们完全可以将这三者分成3行来独立执行，因为它们之间是相互独立的关系。不论三者中每一条命令的结果如何，后面的一条命令总能被得到执行(这是与下文 &amp;&amp; 和 || 的不同之处)。</span><br><span class="line"></span><br><span class="line">5.1.2 &amp;&amp;</span><br><span class="line"></span><br><span class="line">&amp;&amp; 作为组合命令之一，与 &amp; 类似，也有着并列多条命令并将其按顺序执行的功能。与 &amp; 的不同之处，也许此时您已经猜到了，没错，如果多命令中的某一条命令执行出错时，后面的所有命令将不会再被执行；如果一直没有出错则会一直执行完所有的并列命令。为了很好的对比它们之间的区别，我们分别尝试一下下面的两个例子。</span><br><span class="line"><span class="built_in">echo</span> Checking <span class="keyword">if</span> we have the following directory... &amp; dir <span class="string">&quot;E:\starcraft II&quot;</span> &amp; <span class="built_in">echo</span> Seems it does not exist.</span><br><span class="line"><span class="built_in">echo</span> Checking <span class="keyword">if</span> we have the following directory... &amp;&amp; dir <span class="string">&quot;E:\starcraft II&quot;</span> &amp;&amp; <span class="built_in">echo</span> Seems it does not exist.</span><br><span class="line">它们的区别是一目了然的，前者会有 Seems its not exist. 的输出，而后者却没有。</span><br><span class="line"></span><br><span class="line">5.1.3 ||</span><br><span class="line"></span><br><span class="line">|| 的用途与 &amp;&amp; 的功能恰好相反。当遇到执行正确的命令后将不再执行后面的命令，如果没有出现正确的命令则一直执行完所有命令。例如 dir D:\<span class="built_in">test</span> || md D:\<span class="built_in">test</span> ，如果 D:\<span class="built_in">test</span> 存在，即第一条命令执行正确的话，后面的创建 D:\<span class="built_in">test</span> 就不会再执行了；相反，如果第一条命令执行出错，那么后面的命令就起作用了。</span><br><span class="line"></span><br><span class="line">在混合使用的时候需要注意它们的优先级。分析下面几个例子有助于我们理解它们的执行效果(如果我们的机器上并没有 Z: 盘的话)。注：浅蓝色的命令表示会被执行到，而深蓝色的命令将不会被执行到。</span><br><span class="line">dir Z: &amp;　dir C: || <span class="built_in">echo</span> Howdy</span><br><span class="line">dir C: &amp;　dir Z: || <span class="built_in">echo</span> Howdy</span><br><span class="line">dir Z: &amp;&amp; dir C: || <span class="built_in">echo</span> Howdy</span><br><span class="line">dir C: &amp;&amp; dir Z: || <span class="built_in">echo</span> Howdy</span><br><span class="line">dir C: &amp;&amp; dir C: || <span class="built_in">echo</span> Howdy</span><br><span class="line"></span><br><span class="line">dir C: || <span class="built_in">echo</span> Howdy &amp;　<span class="built_in">echo</span> Hi there</span><br><span class="line">dir C: || <span class="built_in">echo</span> Howdy &amp;&amp; <span class="built_in">echo</span> Hi there</span><br><span class="line">dir Z: || <span class="built_in">echo</span> Howdy &amp;　<span class="built_in">echo</span> Hi there</span><br><span class="line">dir Z: || <span class="built_in">echo</span> Howdy &amp;&amp; <span class="built_in">echo</span> Hi there</span><br><span class="line">实验一下，看看结果是不是跟预期的一样。</span><br><span class="line"></span><br><span class="line">5.2 管道命令</span><br><span class="line"></span><br><span class="line">5.2.1 &gt; 、&gt;&gt;</span><br><span class="line"></span><br><span class="line">它们是输出重定向命令，在第2.2节中已有详细的介绍。其主要功能就是将一条命令或某个程序输出结果的重定向到特定文件中。&gt; 与 &gt;&gt; 的区别在于，&gt; 会清除调原有文件中的内容后写入指定文件，而 &gt;&gt; 只会追加内容到指定文件中，而不会改动其中的内容。下面将会是一个很有用的例子。</span><br><span class="line"></span><br><span class="line">众所周知，System32 文件夹是多数木马潜伏和发作的好地方。当我们刚装好机器的时候，可以给此时机器里还没有病毒、木马的 System32 文件夹里的所有可执行文件(.exe)和动态链接库文件(.dll)作个记录。等以后发觉 System32 里多了可疑的东西的时候再作个记录，然后跟之前的记录对比一下，就很容易发现问题了。</span><br><span class="line">为了方便作记录，我们可以执行类似下面的一条命令：dir %windir%\system32\*.exe&gt;D:\%DATE:~0,10%的exe文件.txt 。其中，%windir% 是当前启动系统所在的目录，默认情况下通常是 C:\WINDOWS ；%DATE:~0,10% 是指当天的日期，比如2007-11-30，如果您没有忘记在3.1.4节里曾描述过的话。而整条命令的结果就是把 System32 里的所有可执行文件名称及信息记录到一个指定的文本文件里了。同理：我们可以记录 System32 里的所有 dll 文件：dir %windir%\system32\*.dll&gt;D:\%DATE:~0,10%的dll文件.txt 。</span><br><span class="line">在经过一段时间后，我们可以再次使用上面两条命令，从而得到两个新的记录文件。然后对比一下两个文件看看有什么差异。DOS 命令里提供了这样一条命令 <span class="built_in">fc</span> ，它允许我们对两个文件之间的差异进行比较。使用时就像：<span class="built_in">fc</span> d:\2007-11-30的exe文件.txt d:\2007-12-01的exe文件.txt 。[1]</span><br><span class="line"></span><br><span class="line">5.2.2 |</span><br><span class="line"></span><br><span class="line">没错，只是一条竖线而已。它可以将它左边命令的输出结果放到它右边的命令里作为输入参数。这种用法据说在 Unix 里很常见。</span><br><span class="line"></span><br><span class="line">有个能简单检测机器是否中冰河木马的例子：netstat /a /n | find <span class="string">&quot;7626&quot;</span> &amp;&amp; <span class="built_in">echo</span> 已被冰河感染 || <span class="built_in">echo</span> 未被冰河感染 。其中：</span><br><span class="line">netstat 用于显示当前的网络连接情况，参数 /a 显示所有连接和监听端口；/n 以数字形式显示地址和端口号。仅仅执行 netstat /a /n 后，您将会看到输出的结果为：各种协议下的本地和目标的地址及端口，以及它们的连接状态。</span><br><span class="line">命令 find 则可以搜索指定的字符串，在指定的文件中。netstat /a /n 的输出结果将通过 | 成为命令 find 的第二个参数。因此整个的一条命令可以理解为：在网络连接状态的输出结果中，查找字符串 7626 ，如果查找成功的话，将输出被感染的提示，否则便提示未感染。&amp;&amp; 和 || 请参考第5.1.3节的例子。(端口 7626 是冰河所使用的默认端口。事实上，这并不是一个严谨的检测冰河木马存在的方法，但它却是说明组合命令和管道命令一个很好的例子)</span><br><span class="line"></span><br><span class="line">5.2.3 管道命令还有 &lt; 、&lt;&amp; 和 &gt;&amp; ，它们并不常见，在此暂时不讨论了。</span><br></pre></td></tr></table></figure>

<h2><span id="第六章常用实例-上">第六章：常用实例 上</span></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br></pre></td><td class="code"><pre><span class="line">6.1 批量修改文件名</span><br><span class="line"></span><br><span class="line">在第4.2.2节中，我们已经会使用循环命令对大量文件改名进行批量处理。但总结一下，该批处理并不是很健壮。判断一个程序的好坏，往往不是站在程序员的角度，而从用户的角度出发。比如：在用户使用它的时候，如果输入了不正确的路径格式怎么办？如果输入了含有非法符号的前缀怎么办？输入的扩展名也有问题怎么办？改完名后看不到是否执行成功的反馈信息，等等。带着这些想法，我们将原程序再次修改一下。</span><br><span class="line"></span><br><span class="line">:::::::批量修改文件名.bat:::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">title 批量修改文件名</span><br><span class="line">setlocal EnableDelayedExpansion</span><br><span class="line">:: 启用延迟变量扩充</span><br><span class="line"></span><br><span class="line">:GetPath</span><br><span class="line"><span class="built_in">set</span> zpath=%CD%</span><br><span class="line">:: 对变量进行初始化，防止用户不输入而直接跳过。其中%CD%表示当前路径</span><br><span class="line"><span class="built_in">set</span> /p zpath=请输入目标文件所在的路径：</span><br><span class="line"><span class="keyword">if</span> %zpath:~0,1%%zpath:~-1%==<span class="string">&quot;&quot;</span> <span class="built_in">set</span> zpath=%zpath:~1,-1%</span><br><span class="line">:: 检查变量 zpath 的第一个和最后一个字符是否为 <span class="string">&quot;&quot;</span> ，是的话就去掉</span><br><span class="line"><span class="keyword">if</span> not exist <span class="string">&quot;%zpath%&quot;</span> goto :GetPath</span><br><span class="line">:: 如果 zpath 值的路径不存在，就得跳转回去，要求重新输入</span><br><span class="line"></span><br><span class="line">:GetPrefix</span><br><span class="line"><span class="built_in">set</span> prefix=未命名</span><br><span class="line"><span class="built_in">set</span> /p prefix=请输入文件名前缀(不能包含以下字符\/:*?<span class="string">&quot;&lt;&gt;|)：</span></span><br><span class="line"><span class="string">for /f &quot;</span>delims=\/:*?&lt;&gt;| tokens=2<span class="string">&quot; %%i in (&quot;</span>z%prefix%z<span class="string">&quot;) do goto :GetPrefix</span></span><br><span class="line"><span class="string">:: 这里对变量 perfix 进行检查，发现有非法符号便跳转到 :GetPrefix</span></span><br><span class="line"><span class="string">:: 事实上，这里并没有对双引号 &quot;</span> 进行检测，因为双引号无法在此被转义为可用的分隔符</span><br><span class="line">:: 即使是在这个程序里，不正确地使用双引号也会引起程序异常而退出。</span><br><span class="line">:: 因此，想把它做的非常人性化并不是一件容易的事情</span><br><span class="line"></span><br><span class="line">:GetExt</span><br><span class="line"><span class="built_in">set</span> ext=.*</span><br><span class="line"><span class="built_in">set</span> /p ext=请输入文件的扩展名(不输入则表示所有类型)：</span><br><span class="line"><span class="keyword">if</span> not <span class="string">&quot;%ext:~0,1%&quot;</span>==<span class="string">&quot;.&quot;</span> <span class="built_in">set</span> ext=.%ext%</span><br><span class="line">:: 检查变量 ext 的第一个是否为句点 . ，不是的话就加上</span><br><span class="line">:: 建议这里对变量 ext 也检查一下，发现有除*外的非法符号便跳转到 :GetExt</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> answer=N</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">echo</span> 您试图将 %zpath%\ 里的所有 %ext% 类型的文件以 %prefix% 为前缀名进行批量改名，是否继续？</span><br><span class="line"><span class="built_in">set</span> /p answer=继续请输入 Y ，输入其它键放弃...</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%answer%&quot;</span>==<span class="string">&quot;Y&quot;</span> goto :ReadyToRename</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%answer%&quot;</span>==<span class="string">&quot;y&quot;</span> goto :ReadyToRename</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 放弃文件改名，按任意键退出... &amp; goto :PauseThenQuit</span><br><span class="line"></span><br><span class="line">:ReadyToRename</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a num=0</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%ext%&quot;</span>==<span class="string">&quot;.*&quot;</span> (</span><br><span class="line"><span class="keyword">for</span> %%i <span class="keyword">in</span> (<span class="string">&quot;%zpath%\*%ext%&quot;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> /a num+=1</span><br><span class="line">ren <span class="string">&quot;%%i&quot;</span> <span class="string">&quot;%prefix%!num!%%~xi&quot;</span> || <span class="built_in">echo</span> 文件 %%i 改名失败 &amp;&amp; <span class="built_in">set</span> /a num-=1</span><br><span class="line">)</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="keyword">for</span> %%i <span class="keyword">in</span> (<span class="string">&quot;%zpath%\*%ext%&quot;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> /a num+=1</span><br><span class="line">ren <span class="string">&quot;%%i&quot;</span> <span class="string">&quot;%prefix%!num!%ext%&quot;</span> || <span class="built_in">echo</span> 文件 %%i 改名失败 &amp;&amp; <span class="built_in">set</span> /a num-=1</span><br><span class="line">)</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> %num%==0 <span class="built_in">echo</span> %zpath%\ 里未发现任何文件。按任意键退出... &amp; goto :PauseThenQuit</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 文件改名完成，按任意键退出...</span><br><span class="line"></span><br><span class="line">:PauseThenQuit</span><br><span class="line">pause&gt;nul</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">相对第4.2.2节里的批量修改文件的批处理来说，已经全面多了。不过这仍然有许多地方需要进一步完善，比如，输出的文件名编号可以用001、002、003这样的方式来表达，以便浏览器在以文件名排列文件时能按我们或需要的顺序进行排列。</span><br><span class="line"></span><br><span class="line">========================================朴实的分割线========================================</span><br><span class="line"></span><br><span class="line">6.2 批量备份进程映像列表以及注册表自启动项</span><br><span class="line"></span><br><span class="line">什么是备份；为什么要备份；怎么去备份；备份些什么，说起来我们可能会更关心这些问题。</span><br><span class="line">什么是备份：将某事物复制出额外一份完全一样的事物，并妥善保存起来的过程；</span><br><span class="line">为啥要备份：当原事物出现异常或无法使用的时候，取出复制品并代替原来无法使用的事物；</span><br><span class="line">怎么去备份：复制，与原事物完全一样地去复制，然后保存到安全的地方；</span><br><span class="line">备份些什么：系统、文档、数据库、工程、记录、进度等等。</span><br><span class="line">不过，这些都不是本文的主旨。</span><br><span class="line"></span><br><span class="line">本节的写作起因是从《利用Windows系统自带命令手工搞定病毒》这篇文章开始的。这篇文章的主旨是：用 tasklist 备份好进程列表→通过 <span class="built_in">fc</span> 比较文件找出病毒→用 netstat 判断进程→用 ntsd 终止进程→搜索找出病毒以及同伙文件并删除→用 reg 命令修复注册表。</span><br><span class="line">不过，这些也不是本文的主旨。</span><br><span class="line"></span><br><span class="line">在前面提到的《手工杀毒》一文中，第二步提到：如果感觉机器异常，可以将现有的进程与以前机器正常时的进程加以对比，看看是否多出了可疑的进程。这就意味着您必须得记忆或保留住以前正常时的进程映像名，即备份。备份时正如《手工杀毒》中所说的，在系统正常且刚进入Windows的时候就做一个进程映像名列表的备份。查看机器当前的进程可以通过组合键 Ctrl+Alt+Del 召唤出<span class="string">&quot;Windows 任务管理器&quot;</span>，然后切换到<span class="string">&quot;进程&quot;</span>页即可。对于如何记录下当前的这些进程，您可以用 PrintScreen 键抓图(Alt+PrintScreen 可以仅抓取当前窗口的图)并以图片的形式保存，当然，您甚至可以简单地将这些映像名抄到一张纸上。本文所提供的方法要比抓图或是手抄的方法更先进一些，至少是更严肃了一些。</span><br><span class="line"></span><br><span class="line">除了 Windows 任务管理器 以外，使用命令 tasklist 也可以看到(详细信息请参阅 tasklist/?)当前进程。tasklist&gt;进程.txt 即可将进程以表格的形式输出到一个文本文件中。在输出结果中除了进程映像名以外，还有 PID 、会话、内存使用等信息，但后面的这些信息并不总是固定的，因此我们也并不需要这些。我们所关心的只是一个按照字母顺序排列的进程映像名清单，以便今后进行比较。</span><br><span class="line"></span><br><span class="line">::::::备份进程和自启动.bat::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> TempFolder=临时文件夹</span><br><span class="line">:: 设置临时文件夹的名称</span><br><span class="line"><span class="keyword">if</span> not exist %TempFolder% md %TempFolder%</span><br><span class="line">:: 创建临时文件夹</span><br><span class="line"><span class="built_in">echo</span>.&gt;%TempFolder%\temp.tmp</span><br><span class="line">:: 在临时文件夹里创建一个临时文件 temp.tmp ，并清空其内容</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> ExportImageName=%<span class="built_in">cd</span>%\进程映像列表%date:~0,10%.txt</span><br><span class="line">:: 设置默认导出文件的路径以及文件名</span><br><span class="line"><span class="built_in">set</span> /p ExportImageName=所要导出的进程映像名称列表备份文件名：</span><br><span class="line">:: 用户自定义输入的路径文件名</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;delims=,&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&#x27;tasklist /nh /fo csv&#x27;</span>) <span class="keyword">do</span> <span class="built_in">echo</span> %%~i&gt;&gt;%TempFolder%\temp.tmp</span><br><span class="line">:: 将 tasklist 中的进程映像名输出到临时文件 temp.tmp 中 [注1]</span><br><span class="line">sort %TempFolder%\temp.tmp&gt;<span class="string">&quot;%ExportImageName%&quot;</span></span><br><span class="line">:: 将进程映像名按字母顺序排列</span><br><span class="line">del %TempFolder%\temp.tmp</span><br><span class="line">:: 删除临时文件夹里的文件 temp.tmp</span><br><span class="line"><span class="built_in">echo</span> 当前进程中的所有映像名已导出到以下文件：%ExportImageName%</span><br><span class="line">pause</span><br><span class="line">:::::::: 以上完成了进程备份，下面开始备份自启动的注册表信息</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> ExportRegRunName=%<span class="built_in">cd</span>%\注册表自启动%date:~0,10%.txt</span><br><span class="line">:: 设置默认导出文件的路径以及文件名</span><br><span class="line"><span class="built_in">set</span> /p ExportRegRunName=所要导出的注册表自启动备份文件名：</span><br><span class="line">:: 用户自定义输入的路径文件名</span><br><span class="line"></span><br><span class="line">reg <span class="built_in">export</span> HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run %TempFolder%\HKLMrun.reg</span><br><span class="line">reg <span class="built_in">export</span> HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run %TempFolder%\HKLMexp.reg</span><br><span class="line">reg <span class="built_in">export</span> HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run %TempFolder%\HKCUrun.reg</span><br><span class="line">reg <span class="built_in">export</span> HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run %TempFolder%\HKCUexp.reg</span><br><span class="line">:: 将相关的注册表信息导出到各个临时的注册表文件中 [注2]</span><br><span class="line"></span><br><span class="line">copy %TempFolder%\*.reg <span class="string">&quot;%ExportRegRunName%&quot;</span>&gt;nul</span><br><span class="line">:: 将临时文件夹里所有的注册表文件合并复制到一个文件中</span><br><span class="line">del %TempFolder%\*.reg</span><br><span class="line">:: 删除临时文件夹里所有的临时注册表文件</span><br><span class="line"><span class="keyword">if</span> exist %TempFolder% rd %TempFolder%</span><br><span class="line">:: 删除临时文件夹</span><br><span class="line"><span class="built_in">echo</span> 当前自启动的注册表信息已导出到以下文件：%ExportRegRunName%</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">注1. tasklist /nh /fo csv 中参数 /nh 表示不输出栏标头，参数 /fo csv 表示以双引号包含字符串，并用逗号分隔各个字符串。<span class="keyword">for</span> /f <span class="string">&quot;delims=,&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&#x27;tasklist /nh /fo csv&#x27;</span>) <span class="keyword">do</span> <span class="built_in">echo</span> %%~i&gt;&gt;a.txt 表示在无栏标头且以 csv 格式输出的 tasklist 结果中，以逗号为分隔符，将每行的首字符串依次输出到文本文件 a.txt 中。</span><br><span class="line"></span><br><span class="line">注2. reg <span class="built_in">export</span> 能将注册表指定项的所有子项和值导出到指定的文件中(请参阅 reg/? 与 reg <span class="built_in">export</span> /?)。当然，以上4个只是常见的自启动键值，病毒或木马所在注册表中添加或修改的项不仅此而已，更多需要备份的项可用类似的方法自行添加。</span><br><span class="line"></span><br><span class="line">对于查找可以进程的方法，与前文 5.2.1 小节中所提到的对比系统文件夹找差异的过程类似，使用命令 <span class="built_in">fc</span> 对比两个不同时期的进程映像名列表，找出多余的可疑进程(如果您无法确定某个进程是干什么用的或者是否存在安全风险，可以先到网上搜索并了解一下，比如：进程知识库)。至于是用 taskkill 还是 ntsd 来终止可以的进程，怎样用属性加时间来查找并锁定可以的文件，以及 reg import 注册表导入恢复的用法，这些仍然不是本文的重点。[创作日期：2008-01-08]</span><br><span class="line"></span><br><span class="line">========================================朴实的分割线========================================</span><br><span class="line"></span><br><span class="line">6.3 批量查看同一子网络下的所有IP在线情况[编辑中]</span><br><span class="line"></span><br><span class="line">本小节的批处理可以让您知道自己所在局域网的同一网段下都有哪些IP被使用了。不得不承认，我在创造这个批处理的时候想法很奇怪，甚至有些愚蠢。</span><br><span class="line"></span><br><span class="line">:::::::查看所有子网IP.bat:::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">title 查看所有子网IP</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a Online=0</span><br><span class="line"><span class="built_in">set</span> /a Offline=0</span><br><span class="line"><span class="built_in">set</span> /a Total=256</span><br><span class="line"><span class="built_in">set</span> ExportFile=子网IP在线统计.txt</span><br><span class="line">:: 初始化在线IP与不在线IP的个数为零，共扫描256个IP，结果输出的文件名</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> StartTime=%time%</span><br><span class="line">:: 记录程序的开始时间</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;delims=: tokens=2&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&#x27;ipconfig /all ^| find /i &quot;IP Address&quot;&#x27;</span>) <span class="keyword">do</span> <span class="built_in">set</span> IP=%%i</span><br><span class="line">:: 获得本机IP [注1]</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%IP%&quot;</span>==<span class="string">&quot;&quot;</span> <span class="built_in">echo</span> 未连接到网络 &amp; pause &amp; goto :EOF</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%IP%&quot;</span>==<span class="string">&quot; 0.0.0.0&quot;</span> <span class="built_in">echo</span> 未连接到网络 &amp; pause &amp; goto :EOF</span><br><span class="line">:: 当IP为空或 0.0.0.0 时，提示未连接并退出该程序</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;delims=. tokens=1,2,3,4&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&quot;%IP%&quot;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> /a IP1=%%i</span><br><span class="line"><span class="built_in">set</span> /a IP2=%%j</span><br><span class="line"><span class="built_in">set</span> /a IP3=%%k</span><br><span class="line"><span class="built_in">set</span> /a IP4=%%l</span><br><span class="line">)</span><br><span class="line">:: 以句点为分隔符，分别将IP的四个十进制数赋给四个变量</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a IP4=0</span><br><span class="line"><span class="built_in">echo</span> 在线的IP：&gt;%ExportFile%</span><br><span class="line">:: 初始化IP的第四个数值为零，并创建结果输出文件</span><br><span class="line"></span><br><span class="line">:RETRY</span><br><span class="line">ping %IP1%.%IP2%.%IP3%.%IP4% -n 1 -w 200 -l 16&gt;nul &amp;&amp; <span class="built_in">set</span> /a Online+=1 &amp;&amp; <span class="built_in">echo</span> %IP1%.%IP2%.%IP3%.%IP4%&gt;&gt;%ExportFile% || <span class="built_in">set</span> /a Offline+=1</span><br><span class="line">:: ping 目标IP [注2]</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /p =[将本文底部评论4中的退格符替换到此处]&lt;nul</span><br><span class="line"><span class="built_in">set</span> /a Scanned=%Online%+%Offline%</span><br><span class="line"><span class="built_in">set</span> /a Progress=(%Online%+%Offline%)*100/%Total%</span><br><span class="line"><span class="built_in">set</span> /p =正在扫描：%Scanned%/%Total% 扫描进度：%Progress%%%&lt;nul</span><br><span class="line">:: 删除当前行的内容，并重新显示进度信息 [注3]</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a IP4+=1</span><br><span class="line"><span class="keyword">if</span> %IP4% lss %Total% goto :RETRY</span><br><span class="line">:: 当IP的第四个数值小于总数时，跳转回 :RETRY 处，重复执行直到全部 ping 完为止</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> EndTime=%time%</span><br><span class="line">:: 记录程序的结束时间</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a Seconds = %EndTime:~6,2% - %StartTime:~6,2%</span><br><span class="line"><span class="built_in">set</span> /a Minutes = %EndTime:~3,2% - %StartTime:~3,2%</span><br><span class="line"><span class="keyword">if</span> %Seconds% lss 0 <span class="built_in">set</span> /a Seconds += 60 &amp; <span class="built_in">set</span> /a Minutes -= 1</span><br><span class="line"><span class="keyword">if</span> %Minutes% lss 0 <span class="built_in">set</span> /a Minutes += 60</span><br><span class="line">:: 计算时间差</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a Percent=%Online%*100/(%Online%+%Offline%)</span><br><span class="line">:: 计算在线百分比</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 在线IP个数: %Online%</span><br><span class="line"><span class="built_in">echo</span> 不在线IP个数: %Offline%</span><br><span class="line"><span class="built_in">echo</span> 在线百分比: %Percent%%%</span><br><span class="line"><span class="built_in">echo</span> 统计耗时: %Minutes%分%Seconds%秒</span><br><span class="line"><span class="built_in">echo</span> 统计日期: %date% %time:~0,-3%</span><br><span class="line"><span class="built_in">echo</span>.&gt;&gt;%ExportFile%</span><br><span class="line"><span class="built_in">echo</span> 在线IP个数: %Online%&gt;&gt;%ExportFile%</span><br><span class="line"><span class="built_in">echo</span> 不在线IP个数: %Offline%&gt;&gt;%ExportFile%</span><br><span class="line"><span class="built_in">echo</span> 在线百分比: %Percent%%%&gt;&gt;%ExportFile%</span><br><span class="line"><span class="built_in">echo</span> 统计耗时: %Minutes%分%Seconds%秒&gt;&gt;%ExportFile%</span><br><span class="line"><span class="built_in">echo</span> 统计日期: %date% %time:~0,-3%&gt;&gt;%ExportFile%</span><br><span class="line"><span class="built_in">echo</span> 记录已保存到文件<span class="string">&quot;%ExportFile%&quot;</span>中</span><br><span class="line">::显示结果并将结果保存到文件中</span><br><span class="line">pause</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">注1. ipconfig 是内置于 Windows 的 TCP/IP 应用程序，用于显示本地计算机网络适配器的物理地址和IP地址等配制信息，这些信息一般用来检验手动配置的 TCP/IP 设置是否正确。当在网络中使用 DHCP 服务时， ipconfig 可以检测到计算机中分配到了什么IP地址，是否配置正确，并且可以释放，重新获取IP地址。这些信息对于网络测试和故障排除都有重要的作用。[3]</span><br><span class="line">更详细的说明请参阅 ipconfig/? 。ipconfig /all ，参数 /all 表示查看详细的网络配置。命令 ipconfig /all ^| find /i <span class="string">&quot;IP Address&quot;</span> 表示在 <span class="string">&#x27;ipconfig /all 的结果中，以 &quot;IP Address&quot; 为查找对象，进行搜索(其结果类似于：IP Address. . . . . . . . . . . . : 10.30.11.51 )。</span></span><br><span class="line"><span class="string">而整条命令中的 for 语句，则表示在上述结果中，以冒号为间隔(delims=:)，查找第2个字串(tokens=2)。很明显，所找到的结果就是自己电脑当前的IP地址了(如果您只有一快网卡或是只启用了一个网卡的话。显然，对于多个网卡会显示出多个IP的情况，我并没有考虑的太全面)。[关于 for 更详细请参阅 4.2.4 小节]</span></span><br><span class="line"><span class="string">另外，注意到在 ipconfig /all ^| find /i &quot;IP Address&quot; 中有一个转义字符 ^ ，它的作用是让后面的管道命令 | 生效，而不是让程序把 | 误解为 for 语句里参数的一部分。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">注2. ping 其实才是本批处理的核心部分。命令 ping 的主要作用是通过发送数据包并接收应答信息来检测两台计算机之间的网络是否连通。比如我可以输入 ping 10.30.11.35 以便查看我是否能与我所在的局域网中IP为 10.30.11.35 的机器连通。如果我不懂批处理的话，也许我就得从 IP 10.30.11.1 开始，挨个地 ping 到 IP 10.30.11.255 ，才能达到我在本小节的最初目的。</span></span><br><span class="line"><span class="string">在批处理中 ping 的3个参数 -n 1 -w 200 -l 16 分别表示：仅 ping 一遍[-n 1]，等待200毫秒后按超时考虑[-w 200]，发送16字节的数据[-l 16]。</span></span><br><span class="line"><span class="string">另外，此命令行中同时用到了两个 &amp;&amp; 和一个 || 的组合命令，我不得不承认这种复杂的逻辑关系会给您带来阅读上的困难。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">注3. 这里使用了 set /p =显示内容&lt;nul 来显示一些内容，是因为这中方式显示出内容后并不换行，而 echo 却不行。还有向上数4行的那一堆奇怪的符号，表示的是退格符号，能删除掉当前行中以显示的内容。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">图6-3 查看所有子网IP.bat 的运行结果</span></span><br><span class="line"><span class="string">本小节的使用程度并不大，却很有趣，至少并没有想象中的那么愚蠢。[创作日期：2008-01-10]</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">========================================朴实的分割线========================================</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">6.4 令人大囧的WGA，华丽地卸载之</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">WGA 全称 Windows Genuine Advantage ，是微软制造的反盗版软件，因其自动潜入系统而备受争议。当用户访问 Windows Update 服务以及微软网站的下载页面时，该计划的附属检测软件就会被请求安装。很多用户都是在不知情的情况下被安装上了WGA。事实上，安装了WGA后，正版的用户并没见得有什么优势，但盗版用户的劣势绝对会被发挥的淋漓尽致。如果您是盗版的受害者(尽管您更坚持认为自己是WGA的受害者)，那么建议您仔细阅读一下本小节。首先，卸载方法在微软的帮助与支持中有详细英文说明，中文版的在百度知道里也有介绍。不过，这些依然不是本文的主旨，我们更关心的还是批处理。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">::::::::::WGA卸载器.bat:::::::::</span></span><br><span class="line"><span class="string">@echo off</span></span><br><span class="line"><span class="string">title WGA卸载器</span></span><br><span class="line"><span class="string">setlocal EnableDelayedExpansion</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">set /a step=1</span></span><br><span class="line"><span class="string">if exist WGA卸载记录.txt (</span></span><br><span class="line"><span class="string">for /f &quot;tokens=2&quot; %%i in (WGA卸载记录.txt) do set /a step=%%i+1 &amp; goto :Step!step!</span></span><br><span class="line"><span class="string">)</span></span><br><span class="line"><span class="string">:: 查看记录进行到第几步了</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:Step1</span></span><br><span class="line"><span class="string">:RenameFiles</span></span><br><span class="line"><span class="string">if exist %Windir%\system32\wgalogon.old del %Windir%\system32\wgalogon.old</span></span><br><span class="line"><span class="string">if exist %Windir%\system32\wgatray.old del %Windir%\system32\wgatray.old</span></span><br><span class="line"><span class="string">:: 检查相关的临时文件是否已存在，若存在则删之</span></span><br><span class="line"><span class="string">Ren %Windir%\system32\WgaLogon.dll WgaLogon.old</span></span><br><span class="line"><span class="string">if %errorlevel% neq 0 goto :Abort</span></span><br><span class="line"><span class="string">Ren %Windir%\system32\WgaTray.exe WgaTray.old</span></span><br><span class="line"><span class="string">if %errorlevel% neq 0 goto :Abort</span></span><br><span class="line"><span class="string">:: 将相关文件改名，如果操作失败则中断程序</span></span><br><span class="line"><span class="string">call :WriteUninstallStatus</span></span><br><span class="line"><span class="string">:: 将卸载情况记录到文件中</span></span><br><span class="line"><span class="string">echo 步骤一完成。已重命名文件 WgaLogon.dll 和 WgaTray.exe。</span></span><br><span class="line"><span class="string">ping -n 2 127.0&gt;nul</span></span><br><span class="line"><span class="string">goto :RebootPrompt</span></span><br><span class="line"><span class="string">:: 需要重启计算机</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:Step2</span></span><br><span class="line"><span class="string">:UnregisterServer</span></span><br><span class="line"><span class="string">Regsvr32 %Windir%\system32\LegitCheckControl.dll /u</span></span><br><span class="line"><span class="string">:: 将相关的服务注册撤消</span></span><br><span class="line"><span class="string">call :WriteUninstallStatus</span></span><br><span class="line"><span class="string">:: 将卸载情况记录到文件中</span></span><br><span class="line"><span class="string">echo 步骤二完成。已撤消服务 LegitCheckControl.dll。</span></span><br><span class="line"><span class="string">ping -n 2 127.0&gt;nul</span></span><br><span class="line"><span class="string">goto :RebootPrompt</span></span><br><span class="line"><span class="string">:: 需要重启计算机</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:Step3</span></span><br><span class="line"><span class="string">:DeleteFiles</span></span><br><span class="line"><span class="string">Del %Windir%\system32\wgalogon.old</span></span><br><span class="line"><span class="string">Del %Windir%\system32\WgaTray.old</span></span><br><span class="line"><span class="string">Del %Windir%\system32\LegitCheckControl.dll</span></span><br><span class="line"><span class="string">:: 将相关的文件删除</span></span><br><span class="line"><span class="string">call :WriteUninstallStatus</span></span><br><span class="line"><span class="string">:: 将卸载情况记录到文件中</span></span><br><span class="line"><span class="string">echo 步骤三完成。已删除文件 wgalogon.old WgaTray.old 和 LegitCheckControl.dll。</span></span><br><span class="line"><span class="string">ping -n 2 127.0&gt;nul</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:Step4</span></span><br><span class="line"><span class="string">:DeleteRegistrySubkeys</span></span><br><span class="line"><span class="string">set /a step=4</span></span><br><span class="line"><span class="string">reg delete &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify\WgaLogon&quot; /f</span></span><br><span class="line"><span class="string">reg delete &quot;HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WgaNotify&quot; /f</span></span><br><span class="line"><span class="string">:: 将相关的子项从注册表中删除</span></span><br><span class="line"><span class="string">call :WriteUninstallStatus</span></span><br><span class="line"><span class="string">echo 步骤四完成。已删除注册表中相关的子项。</span></span><br><span class="line"><span class="string">:: 将卸载情况记录到文件中</span></span><br><span class="line"><span class="string">ping -n 2 127.0&gt;nul</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:Step5</span></span><br><span class="line"><span class="string">:UninstallDoneSuccessfully</span></span><br><span class="line"><span class="string">echo.</span></span><br><span class="line"><span class="string">echo 已完成对WGA的卸载!</span></span><br><span class="line"><span class="string">del WGA卸载记录.txt</span></span><br><span class="line"><span class="string">pause&gt;nul</span></span><br><span class="line"><span class="string">exit</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">::::以下为程序所调用到的函数::::</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:WriteUninstallStatus</span></span><br><span class="line"><span class="string">:: 该函数用于将当前卸载情况记录到文本文件中</span></span><br><span class="line"><span class="string">echo WGA卸载已完成第 %step% 步&gt;WGA卸载记录.txt</span></span><br><span class="line"><span class="string">set /p =更改文件名称：&lt;nul &gt;&gt;WGA卸载记录.txt</span></span><br><span class="line"><span class="string">call :IsCompleted %step%</span></span><br><span class="line"><span class="string">set /p =撤消服务注册：&lt;nul &gt;&gt;WGA卸载记录.txt</span></span><br><span class="line"><span class="string">call :IsCompleted %step%</span></span><br><span class="line"><span class="string">set /p =删除相关文件：&lt;nul &gt;&gt;WGA卸载记录.txt</span></span><br><span class="line"><span class="string">call :IsCompleted %step%</span></span><br><span class="line"><span class="string">set /p =清除注册信息：&lt;nul &gt;&gt;WGA卸载记录.txt</span></span><br><span class="line"><span class="string">call :IsCompleted %step%</span></span><br><span class="line"><span class="string">goto :EOF</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:IsCompleted</span></span><br><span class="line"><span class="string">:: 该函数用于判断各个卸载步骤的完成情况</span></span><br><span class="line"><span class="string">if %1 gtr 0 (</span></span><br><span class="line"><span class="string">echo 已完成&gt;&gt;WGA卸载记录.txt</span></span><br><span class="line"><span class="string">set /a step-=1</span></span><br><span class="line"><span class="string">) else (</span></span><br><span class="line"><span class="string">echo 未完成&gt;&gt;WGA卸载记录.txt</span></span><br><span class="line"><span class="string">)</span></span><br><span class="line"><span class="string">goto :EOF</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:RebootPrompt</span></span><br><span class="line"><span class="string">set /a TimerTick=9</span></span><br><span class="line"><span class="string">echo.</span></span><br><span class="line"><span class="string">echo 需要重启计算机才能继续下一步。</span></span><br><span class="line"><span class="string">echo 请在计算机重启后再运行该批处理，以便继续完成对WGA的卸载。</span></span><br><span class="line"><span class="string">set /p choice=是否现在就重启? [Y/N]</span></span><br><span class="line"><span class="string">if &quot;%choice%&quot;==&quot;y&quot; shutdown -r -t 15 -c 因卸载WGA的需要而重启&amp; exit</span></span><br><span class="line"><span class="string">if &quot;%choice%&quot;==&quot;Y&quot; shutdown -r -t 15 -c 因卸载WGA的需要而重启&amp; exit</span></span><br><span class="line"><span class="string">echo 稍后请手动重启计算机。</span></span><br><span class="line"><span class="string">pause&gt;nul</span></span><br><span class="line"><span class="string">exit</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">:Abort</span></span><br><span class="line"><span class="string">echo 程序中止。</span></span><br><span class="line"><span class="string">echo.</span></span><br><span class="line"><span class="string">echo 您确信您已经安装了WGA?</span></span><br><span class="line"><span class="string">pause&gt;nul</span></span><br><span class="line"><span class="string">exit</span></span><br><span class="line"><span class="string">::::::::::::::::::::::::::::::::</span></span><br></pre></td></tr></table></figure>

<h2><span id="第七章常用实例-下">第七章：常用实例 下</span></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">7.1 坐在家里周游世界</span><br><span class="line"></span><br><span class="line">没错，我想是认真的，对于任何您想去往地球上的地点，您所需要做的仅仅是提供一个目的地的经纬度坐标而已。当然，我是不会给您提供去往世界各地的签证和机票的，但是海量的卫星照片还是能让您过足了瘾的。此时您手头可能并没有足够的坐标资源，不过我这里<span class="string">&quot;恰好&quot;</span>有些陈芝麻烂谷子，是当年在留园网上搜集的地理坐标，具体请参阅《关于Google Maps的趣点》。这里必须得先感谢卫星图片的提供者 Google Maps ，因为你知道，毕竟航拍卫星并不是人人都能买的起的。</span><br><span class="line"></span><br><span class="line">本小节也是在我回顾《关于Google Maps的趣点》这篇文章开始的。因为当年在搜集这些该死的地理坐标时，偷了一些懒，结果有些是小数格式的，有些是度分秒格式的，查看起来并不方便。如果有 Google Earth 来转换它们之间的格式倒也不难。但我知道，此时您的迅雷网快电驴里已经排满了各种各样电影、游戏、动漫的下载任务，看来您并没有下载 Google Earth 的意思。没关系，下面这个批处理多少能让您更方便地浏览 Google Maps 上的卫星照片。</span><br><span class="line"></span><br><span class="line">::::::坐在家里周游世界.bat::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">title 坐在家里周游世界</span><br><span class="line">:: 设置标题</span><br><span class="line">:Start</span><br><span class="line">cls</span><br><span class="line">:: 清屏</span><br><span class="line"><span class="built_in">set</span> choice=1</span><br><span class="line"><span class="built_in">set</span> /p choice=请选择经纬度格式(1. 小数格式 2. 度分秒格式 3. 退出)：</span><br><span class="line">:: 选择经纬度的格式 或 退出程序</span><br><span class="line"><span class="keyword">if</span> %choice%==3 goto :EOF</span><br><span class="line"><span class="keyword">if</span> %choice%==2 goto :DMSFormat</span><br><span class="line"></span><br><span class="line">:DecimalFormat</span><br><span class="line">:: 小数格式的经纬度处理</span><br><span class="line"><span class="built_in">set</span> latitude=39.906477</span><br><span class="line"><span class="built_in">set</span> longitude=116.391467</span><br><span class="line">:: 初始化为伟大首都天安门的坐标</span><br><span class="line"><span class="built_in">set</span> /p latitude=纬度：</span><br><span class="line"><span class="built_in">set</span> /p longitude=经度：</span><br><span class="line">:: 提示用户输入目标的经纬度 [注1]</span><br><span class="line">goto :LaunchMap</span><br><span class="line"></span><br><span class="line">:DMSFormat</span><br><span class="line">:: 度分秒格式的经纬度处理</span><br><span class="line"><span class="built_in">set</span> <span class="string">&quot;DMSlatitude=39 54 23 N&quot;</span></span><br><span class="line"><span class="built_in">set</span> /a degree=39</span><br><span class="line"><span class="built_in">set</span> /a minute=54</span><br><span class="line"><span class="built_in">set</span> /a second=23</span><br><span class="line"><span class="built_in">set</span> NorthOrSouth=N</span><br><span class="line">:: 初始化为美丽首都天安门度分秒格式的纬度</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">set</span> /p DMSlatitude=纬度(格式 [度] [分] [秒] [N ^| S])：</span><br><span class="line">:: 提示用户输入目标的纬度 [注2]</span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;tokens=1,2,3,4,5&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&quot;%DMSlatitude%&quot;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> degree=%%i</span><br><span class="line"><span class="built_in">set</span> minute=%%j</span><br><span class="line"><span class="built_in">set</span> second=%%k</span><br><span class="line"><span class="built_in">set</span> NorthOrSouth=%%l</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%%l&quot;</span>==<span class="string">&quot;&quot;</span> <span class="built_in">echo</span> 不正确的格式 &amp; goto :DMSFormat</span><br><span class="line">)</span><br><span class="line">:: 分别获得纬度的度、分、秒，以及南半球或北半球</span><br><span class="line"><span class="keyword">if</span> %degree% lss 0 <span class="built_in">echo</span> 纬度必须大于0度 或不正确的格式 &amp; goto :DMSFormat</span><br><span class="line"><span class="keyword">if</span> %degree% gtr 90 <span class="built_in">echo</span> 纬度必须小于90度 或不正确的格式 &amp; goto :DMSFormat</span><br><span class="line"><span class="keyword">if</span> %minute% lss 0 <span class="built_in">echo</span> 纬度必须大于0分 或不正确的格式 &amp; goto :DMSFormat</span><br><span class="line"><span class="keyword">if</span> %minute% gtr 60 <span class="built_in">echo</span> 纬度必须小于60分 或不正确的格式 &amp; goto :DMSFormat</span><br><span class="line"><span class="keyword">if</span> %second% lss 0 <span class="built_in">echo</span> 纬度必须大于0秒 或不正确的格式 &amp; goto :DMSFormat</span><br><span class="line"><span class="keyword">if</span> %second% gtr 60 <span class="built_in">echo</span> 纬度必须小于60秒 或不正确的格式 &amp; goto :DMSFormat</span><br><span class="line">:: 判断纬度的度、分、秒格式是否有效 [注3]</span><br><span class="line"><span class="built_in">set</span> /a degree*=3600</span><br><span class="line"><span class="built_in">set</span> /a minute*=60</span><br><span class="line"><span class="built_in">set</span> /a second=%degree%+%minute%+%second%</span><br><span class="line"><span class="built_in">set</span> /a latitude=%second%*2500/9</span><br><span class="line">:: 将度分秒计算并转换为小数格式 [注4]</span><br><span class="line"><span class="keyword">if</span> %NorthOrSouth%==N goto :LatitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %NorthOrSouth%==n goto :LatitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %NorthOrSouth%==S <span class="built_in">set</span> /a latitude=0-%latitude% &amp; goto :LatitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %NorthOrSouth%==s <span class="built_in">set</span> /a latitude=0-%latitude% &amp; goto :LatitudeLockDown</span><br><span class="line">:: 判断南北半球格式是否有效</span><br><span class="line"><span class="built_in">echo</span> 南北半球标识不明确，请使用 N 或 S 来表示，不区分大小写。 &amp; goto :DMSFormat</span><br><span class="line"></span><br><span class="line">:LatitudeLockDown</span><br><span class="line"><span class="built_in">set</span> <span class="string">&quot;DMSlongitude=116 23 29 E&quot;</span></span><br><span class="line"><span class="built_in">set</span> /a degree=116</span><br><span class="line"><span class="built_in">set</span> /a minute=23</span><br><span class="line"><span class="built_in">set</span> /a second=29</span><br><span class="line"><span class="built_in">set</span> EastOrWest=E</span><br><span class="line">:: 初始化为可爱首都天安门度分秒格式的经度</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">set</span> /p DMSlongitude=经度(格式 [度] [分] [秒] [E ^| W])：</span><br><span class="line">:: 提示用户输入目标的经度</span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;tokens=1,2,3,4&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&quot;%DMSlongitude%&quot;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> degree=%%i</span><br><span class="line"><span class="built_in">set</span> minute=%%j</span><br><span class="line"><span class="built_in">set</span> second=%%k</span><br><span class="line"><span class="built_in">set</span> EastOrWest=%%l</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%%l&quot;</span>==<span class="string">&quot;&quot;</span> <span class="built_in">echo</span> 不正确的格式 &amp; goto :LatitudeLockdown</span><br><span class="line">)</span><br><span class="line">:: 分别获得经度的度、分、秒，以及东半球或西半球</span><br><span class="line"><span class="keyword">if</span> %degree% lss 0 <span class="built_in">echo</span> 经度必须大于0度 或不正确的格式 &amp; goto :LatitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %degree% gtr 180 <span class="built_in">echo</span> 经度必须小于180度 或不正确的格式 &amp; goto :LatitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %minute% lss 0 <span class="built_in">echo</span> 经度必须大于0分 或不正确的格式 &amp; goto :LatitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %minute% gtr 60 <span class="built_in">echo</span> 经度必须小于60分 或不正确的格式 &amp; goto :LatitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %second% lss 0 <span class="built_in">echo</span> 经度必须大于0秒 或不正确的格式 &amp; goto :LatitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %second% gtr 60 <span class="built_in">echo</span> 经度必须小于60秒 或不正确的格式 &amp; goto :LatitudeLockDown</span><br><span class="line">:: 判断经度的度、分、秒格式是否有效</span><br><span class="line"><span class="built_in">set</span> /a degree*=3600</span><br><span class="line"><span class="built_in">set</span> /a minute*=60</span><br><span class="line"><span class="built_in">set</span> /a second=%degree%+%minute%+%second%</span><br><span class="line"><span class="built_in">set</span> /a longitude=%second%*2500/9</span><br><span class="line">:: 将度分秒计算并转换为小数格式</span><br><span class="line"><span class="keyword">if</span> %EastOrWest%==E goto :LongitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %EastOrWest%==e goto :LongitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %EastOrWest%==W <span class="built_in">set</span> /a longitude=0-%longitude% &amp; goto :LongitudeLockDown</span><br><span class="line"><span class="keyword">if</span> %EastOrWest%==w <span class="built_in">set</span> /a longitude=0-%longitude% &amp; goto :LongitudeLockDown</span><br><span class="line">:: 判断东西半球格式是否有效</span><br><span class="line"><span class="built_in">echo</span> 东西半球标识不明确，请使用 E 或 W 来表示，不区分大小写。 &amp; goto :LatitudeLockDown</span><br><span class="line"></span><br><span class="line">:LongitudeLockDown</span><br><span class="line"><span class="built_in">set</span> latitude=%latitude:~0,-6%.%latitude:~-6%</span><br><span class="line"><span class="built_in">set</span> longitude=%longitude:~0,-6%.%longitude:~-6%</span><br><span class="line">:: 整理纬度和经度</span><br><span class="line"></span><br><span class="line">:LaunchMap</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">set</span> /a zoom=16</span><br><span class="line"><span class="built_in">set</span> /p zoom=放缩度(0~18 默认值：%zoom%)：</span><br><span class="line">:: 提示用户输入照片的放缩值</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">echo</span> 正在打开 纬度：%latitude% 经度：%longitude% 的卫星照片...</span><br><span class="line">start <span class="string">&quot;正在打开Google Maps...&quot;</span> <span class="string">&quot;http://maps.google.com/?t=k&amp;z=%zoom%&amp;ll=%latitude%,%longitude%&quot;</span></span><br><span class="line">:: 将放缩值和经纬度值作为 Google Maps 链接参数，打开相应的照片 [注5]</span><br><span class="line"></span><br><span class="line">pause</span><br><span class="line">goto :Start</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">注1. 纬度或经度尽量能精确到小数点后3位数以上，因为越精确的坐标越能准确地帮您找到目标的位置</span><br><span class="line"></span><br><span class="line">注2. 度分秒格式的纬度(或经度)您大概并不陌生，如果高中时代的您没有选择在地理课上逃课的话。例如 39°54<span class="string">&#x27; 23.32&quot; N 就表示北纬39度54分23.32秒。另外，正在上高中的朋友在政治课上可以适当地翘课，个人研究表明，政治这玩意学多了不利于青少年大脑的发育。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">注3. 纬度不能超过90度，而经度不能超过180度，分和秒的范围是0~59，这些常识是绝不会难到您的。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">注4. 经纬度从度分秒格式转换为小数格式只需要：(度*60*60+分*60+秒)/60/60 即可，北纬39度54分23.32秒转换为小数即39.906477度。事实上，此处latitude的值是实际纬度的一百万倍，因为DOS命令中并不支持浮点型(实数)的变量，不用担心，在后面会有小数点向前移动6位的处理。同时，希望您也没有在数学课上翘课的习惯。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">注5. 该链接正指向此时经纬度和放缩值的卫星地图，其中具体参数的含义可以参考《关于Google Maps的趣点》文中的解释。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">好吧，您可能已经迫不及待地想试试这东西了。那么在运行该批处理后，首先您会得到选择两种经纬度类型的提示。选1的话，只需要分别输入小数格式的纬度和精度，以及放缩值即可(如果您还不确定放缩值是啥东西的话，可以置空直接使用默认值)。</span></span><br><span class="line"><span class="string">如果在程序的一开始选择了2，也就是度分秒格式的经纬度。比如我们的目的地是：北纬15°17&#x27;</span> 55<span class="string">&quot; ，东经19°25&#x27; 47&quot;</span> ，您可以依次在纬度和经度里输入 15 17 55 n 和 19 25 47 e ，然后在放缩值里填上 22 。这样您就能看到北非中部一个小村庄里的几位村民，以及他们的奶牛和骆驼。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">坐在家里周游世界.bat 的运行界面</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">坐在家里周游世界.bat 的运行结果</span><br><span class="line"></span><br><span class="line">事实上，上面的照片是 Google Maps 中罕见的几张高清卫星照片之一，并不是每张照片的放缩值都能达到23滴，因为你也知道，即使是能买得起航拍卫星的家伙，也是没有足够的资金和精力来把地球的每一个角落都拍得一清二楚的。</span><br><span class="line"></span><br><span class="line">========================================朴实的分割线========================================</span><br><span class="line"></span><br><span class="line">7.2 进程分析者</span><br><span class="line"></span><br><span class="line">写了一篇很简单却又很占篇幅的玩意，被我称之为<span class="string">&quot;进程分析者&quot;</span>。此批处理的构思很轻松，只是简单地使用 tasklist 列出所有的进程，再显示为容易理解的文字说明而已。这与任务管理器中的进程相比，除了易于识别以外，还能帮您鉴别出那些喜欢<span class="string">&quot;偷梁换柱&quot;</span>的隐患进程。比如用肉眼去观察 winhlep.exe 或 winhe1p.exe 的时候很容易忽视它们，而使用<span class="string">&quot;进程分析者&quot;</span>却不会。</span><br><span class="line"></span><br><span class="line">:::::::::进程分析者.bat:::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">setlocal enabledelayedexpansion</span><br><span class="line"></span><br><span class="line">title 进程分析者</span><br><span class="line"><span class="built_in">set</span> SPACE=</span><br><span class="line"><span class="built_in">set</span> /a NumOfTotal=0</span><br><span class="line"><span class="built_in">set</span> /a NumOfSafe=0</span><br><span class="line"><span class="built_in">set</span> /a NumOfNasty=0</span><br><span class="line"><span class="built_in">set</span> /a NumOfUnknown=0</span><br><span class="line"><span class="built_in">set</span> IconOfSafe=√</span><br><span class="line"><span class="built_in">set</span> IconOfNasty=×</span><br><span class="line"><span class="built_in">set</span> IconOfUnknown=？</span><br><span class="line"></span><br><span class="line">:::::::: 以下定义为可信任的进程，可自定义更多的扩充 ::::::::</span><br><span class="line">:: 1. 系统进程</span><br><span class="line"><span class="built_in">set</span> alg.exe=%IconOfSafe%处理Windows网络连接共享和网络连接防火墙[系统进程]</span><br><span class="line"><span class="built_in">set</span> csrss.exe=%IconOfSafe%管理Windows图形相关任务[系统进程]</span><br><span class="line"><span class="built_in">set</span> explorer.exe=%IconOfSafe%用于显示系统桌面的图标,任务栏等[系统进程]</span><br><span class="line"><span class="built_in">set</span> lsass.exe=%IconOfSafe%用于本地安全和登陆策略[系统进程]</span><br><span class="line"><span class="built_in">set</span> services.exe=%IconOfSafe%管理启动和停止服务[系统进程]</span><br><span class="line"><span class="built_in">set</span> smss.exe=%IconOfSafe%用于对话管理子系统调用和系统对话操作[系统进程]</span><br><span class="line"><span class="built_in">set</span> spoolsv.exe=%IconOfSafe%用于将打印机任务发送到本地打印机[系统进程]</span><br><span class="line"><span class="built_in">set</span> svchost.exe=%IconOfSafe%用于执行动态链接库DLL文件[系统进程]</span><br><span class="line"><span class="built_in">set</span> System=%IconOfSafe%[系统进程]</span><br><span class="line"><span class="built_in">set</span> taskmgr.exe=%IconOfSafe%任务管理器，用于显示系统正在运行的进程[系统进程]</span><br><span class="line"><span class="built_in">set</span> winlogon.exe=%IconOfSafe%用于处理系统的登陆和登陆过程[系统进程]</span><br><span class="line"><span class="built_in">set</span> winmgmt.exe=%IconOfSafe%用于系统管理员创建WIndows管理脚本[系统进程]</span><br><span class="line">:: 2. 基本进程</span><br><span class="line"><span class="built_in">set</span> cmd.exe=%IconOfSafe%Windows系统的命令行程序</span><br><span class="line"><span class="built_in">set</span> msimn.exe=%IconOfSafe%OutlookExpress相关程序</span><br><span class="line"><span class="built_in">set</span> mspaint.exe=%IconOfSafe%画图板</span><br><span class="line"><span class="built_in">set</span> notepad.exe=%IconOfSafe%记事本</span><br><span class="line"><span class="built_in">set</span> wab.exe=%IconOfSafe%通讯薄，用于储存地址、联系人和Email</span><br><span class="line"><span class="built_in">set</span> ctfmon.exe=%IconOfSafe%提供语音识别、手写识别等</span><br><span class="line"><span class="built_in">set</span> conime.exe=%IconOfSafe%输入法编辑器相关程序</span><br><span class="line"><span class="built_in">set</span> SOUNDMAN.EXE=%IconOfSafe%Realtek声卡相关程序</span><br><span class="line"><span class="built_in">set</span> tasklist.exe=%IconOfSafe%这是本批处理的核心所在-_-b</span><br><span class="line"><span class="built_in">set</span> wmiprvse.exe=%IconOfSafe%用于通过WinMgmt.exe程序处理WMI操作</span><br><span class="line">:: 3. 工作进程</span><br><span class="line"><span class="built_in">set</span> EXCEL.EXE=%IconOfSafe%Excel</span><br><span class="line"><span class="built_in">set</span> WINWORD.EXE=%IconOfSafe%Word</span><br><span class="line"><span class="built_in">set</span> XDICT.EXE=%IconOfSafe%金山词霸</span><br><span class="line"><span class="built_in">set</span> sqlservr.exe=%IconOfSafe%用于SQL基础服务</span><br><span class="line"><span class="built_in">set</span> wmplayer.exe=%IconOfSafe%Windows Media Player</span><br><span class="line"><span class="built_in">set</span> Mplayerc.exe=%IconOfSafe%暴风影音</span><br><span class="line"><span class="built_in">set</span> WinRAR.exe=%IconOfSafe%WinRAR</span><br><span class="line">:: 4. 防护进程</span><br><span class="line"><span class="built_in">set</span> 360tray.exe=%IconOfSafe%360安全卫士实时监控程序</span><br><span class="line"><span class="built_in">set</span> AntiArp.exe=%IconOfSafe%ARP防火墙</span><br><span class="line"><span class="built_in">set</span> CCenter.exe=%IconOfSafe%瑞星信息中心</span><br><span class="line"><span class="built_in">set</span> RavMonD.exe=%IconOfSafe%瑞星监控程序</span><br><span class="line"><span class="built_in">set</span> rfwsrv.exe=%IconOfSafe%瑞星个人防火墙相关程序</span><br><span class="line"><span class="built_in">set</span> RavStub.exe=%IconOfSafe%瑞星杀毒软件相关程序</span><br><span class="line"><span class="built_in">set</span> RfwMain.exe=%IconOfSafe%瑞星防火墙主程序</span><br><span class="line"><span class="built_in">set</span> RavTask.exe=%IconOfSafe%瑞星定时杀毒程序</span><br><span class="line"><span class="built_in">set</span> RavMon.exe=%IconOfSafe%瑞星监控程序</span><br><span class="line">:: 5. 网络进程</span><br><span class="line"><span class="built_in">set</span> iexplore.exe=%IconOfSafe%IE浏览器</span><br><span class="line"><span class="built_in">set</span> Maxthon.exe=%IconOfSafe%傲游浏览器</span><br><span class="line"><span class="built_in">set</span> BaiduHi.exe=%IconOfSafe%百度Hi</span><br><span class="line"><span class="built_in">set</span> msmsgs.exe=%IconOfSafe%MSN网络聊天工具</span><br><span class="line"><span class="built_in">set</span> QQ.exe=%IconOfSafe%腾迅QQ</span><br><span class="line"><span class="built_in">set</span> TXPlatform.exe=%IconOfSafe%腾迅平台</span><br><span class="line"><span class="built_in">set</span> Thunder5.exe=%IconOfSafe%迅雷下载</span><br><span class="line"><span class="built_in">set</span> Skype.exe=%IconOfSafe%Skype语音聊天</span><br><span class="line"><span class="built_in">set</span> Contentfilter.exe=%IconOfSafe%Skype的相关程序</span><br><span class="line"><span class="built_in">set</span> skypePM.exe=%IconOfSafe%Skype语音聊天</span><br><span class="line"></span><br><span class="line">:::::::: 以下定义为已知的危险进程，可自定义更多的扩充 ::::::::</span><br><span class="line"><span class="built_in">set</span> a.exe=%IconOfNasty%蠕虫</span><br><span class="line"><span class="built_in">set</span> av.exe=%IconOfNasty%蠕虫</span><br><span class="line"><span class="built_in">set</span> blss.exe=%IconOfNasty%木马/拨号器</span><br><span class="line"><span class="built_in">set</span> cmd32.exe=%IconOfNasty%病毒</span><br><span class="line"><span class="built_in">set</span> crss.exe=%IconOfNasty%蠕虫</span><br><span class="line"><span class="built_in">set</span> csrse.exe=%IconOfNasty%病毒/木马</span><br><span class="line"><span class="built_in">set</span> Desktop.exe=%IconOfNasty%木马/病毒/间谍</span><br><span class="line"><span class="built_in">set</span> directs.exe=%IconOfNasty%蠕虫</span><br><span class="line"><span class="built_in">set</span> dllhlp.exe=%IconOfNasty%木马</span><br><span class="line"><span class="built_in">set</span> dllreg.exe=%IconOfNasty%病毒</span><br><span class="line"><span class="built_in">set</span> explore.exe=%IconOfNasty%灰鸽子</span><br><span class="line"><span class="built_in">set</span> explored.exe=%IconOfNasty%蠕虫</span><br><span class="line"><span class="built_in">set</span> optimize.exe=%IconOfNasty%拨号器/广告</span><br><span class="line"><span class="built_in">set</span> pcsvc.exe=%IconOfNasty%间谍</span><br><span class="line"><span class="built_in">set</span> rundll16.exe=%IconOfNasty%木马</span><br><span class="line"><span class="built_in">set</span> run32dll.exe=%IconOfNasty%间谍</span><br><span class="line"><span class="built_in">set</span> scvhost.exe=%IconOfNasty%木马/广告</span><br><span class="line"><span class="built_in">set</span> svchosts.exe=%IconOfNasty%木马</span><br><span class="line"><span class="built_in">set</span> system32.exe=%IconOfNasty%木马</span><br><span class="line"><span class="built_in">set</span> updater.exe=%IconOfNasty%蠕虫</span><br><span class="line"><span class="built_in">set</span> web.exe=%IconOfNasty%病毒/木马</span><br><span class="line"><span class="built_in">set</span> win32.exe=%IconOfNasty%病毒</span><br><span class="line"><span class="built_in">set</span> windows.exe=%IconOfNasty%蠕虫</span><br><span class="line"><span class="built_in">set</span> winlogin.exe=%IconOfNasty%病毒/木马</span><br><span class="line"><span class="built_in">set</span> winstart.exe=%IconOfNasty%间谍/广告</span><br><span class="line"><span class="built_in">set</span> wintsk32.exe=%IconOfNasty%病毒</span><br><span class="line"><span class="built_in">set</span> winupdate.exe=%IconOfNasty%病毒</span><br><span class="line"><span class="built_in">set</span> winxp.exe=%IconOfNasty%病毒</span><br><span class="line"><span class="built_in">set</span> winhlep.exe=%IconOfNasty%病毒</span><br><span class="line"></span><br><span class="line">:::::::: 以下定义为未知的进程 ::::::::</span><br><span class="line"><span class="built_in">set</span> UnknownTask=%IconOfUnknown%未识别的进程</span><br><span class="line"></span><br><span class="line">:: 程序开始</span><br><span class="line"><span class="built_in">echo</span> 进程名称 分析结果</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;tokens=1&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&#x27;tasklist /NH&#x27;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> TaskName=%%i%SPACE%</span><br><span class="line"><span class="built_in">set</span> TaskName=!TaskName:~0,20!</span><br><span class="line"><span class="keyword">if</span> defined %%i (</span><br><span class="line"><span class="built_in">echo</span> !TaskName! !%%i!</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;!%%i:~0,1!&quot;</span>==<span class="string">&quot;%IconOfNasty%&quot;</span> (</span><br><span class="line"><span class="built_in">set</span> /a NumOfNasty+=1</span><br><span class="line">)</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="built_in">echo</span> !TaskName! %UnknownTask%</span><br><span class="line"><span class="built_in">set</span> /a NumOfUnknown+=1</span><br><span class="line">)</span><br><span class="line"><span class="built_in">set</span> /a NumOfTotal+=1</span><br><span class="line">)</span><br><span class="line"><span class="built_in">echo</span> ________________________________________________________________</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">echo</span> 共 %NumOfTotal% 个进程。</span><br><span class="line"><span class="keyword">if</span> %NumOfNasty% gtr 0 (</span><br><span class="line"><span class="built_in">echo</span> %NumOfNasty% 个存在安全隐患的进程！</span><br><span class="line">)</span><br><span class="line"><span class="keyword">if</span> %NumOfUnknown% gtr 0 (</span><br><span class="line"><span class="built_in">echo</span> %NumOfUnknown% 个未知进程。</span><br><span class="line">)</span><br><span class="line">pause&gt;nul</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">结果：</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">图7-3 进程分析者.bat 的运行结果</span><br><span class="line"></span><br><span class="line">分析完毕！</span><br><span class="line"></span><br><span class="line">========================================朴实的分割线========================================</span><br><span class="line"></span><br><span class="line">7.3 公元2738年11月28日是星期几[编写中]</span><br><span class="line"></span><br><span class="line">公元2738和11月28日，是个好日子。我打赌您不知道这天究竟是星期几而且您也并不想知道。</span><br><span class="line"></span><br><span class="line">:::公元2738-11-28是星期几.bat:::[β版]</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">setlocal enabledelayedexpansion</span><br><span class="line"></span><br><span class="line">:Initialization</span><br><span class="line">:: 初始化</span><br><span class="line"><span class="built_in">set</span> TargetDate=%date:~0,4%%date:~5,2%%date:~8,2%</span><br><span class="line"><span class="built_in">set</span> /a Year = 0 &amp; rem 年 范围: 1~9999</span><br><span class="line"><span class="built_in">set</span> /a Mon = 0 &amp; rem 月 范围: 1~12</span><br><span class="line"><span class="built_in">set</span> /a Day = 0 &amp; rem 日 范围: 1~28 29 30 31</span><br><span class="line"><span class="built_in">set</span> /a IsLeapYear = 0 &amp; rem 是否闰年 范围: 0~1</span><br><span class="line"></span><br><span class="line">:PromptEntering</span><br><span class="line">:: 提示输入日期</span><br><span class="line"><span class="built_in">set</span> /p TargetDate=请输入日期(格式: YYYYMMDD 例如: %TargetDate%):</span><br><span class="line"></span><br><span class="line">:: 转换为有效的年月日[注1]</span><br><span class="line"><span class="keyword">if</span> %TargetDate:~0,3% == 000 (</span><br><span class="line"><span class="built_in">set</span> /a Year = %TargetDate:~3,1%</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="keyword">if</span> %TargetDate:~0,2% == 00 (</span><br><span class="line"><span class="built_in">set</span> /a Year = %TargetDate:~2,2%</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="keyword">if</span> %TargetDate:~0,1% == 0 (</span><br><span class="line"><span class="built_in">set</span> /a Year = %TargetDate:~1,3%</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="built_in">set</span> /a Year = %TargetDate:~0,4%</span><br><span class="line">)</span><br><span class="line">)</span><br><span class="line">)</span><br><span class="line"><span class="keyword">if</span> %TargetDate:~4,1% == 0 (</span><br><span class="line"><span class="built_in">set</span> /a Mon = %TargetDate:~5,1%</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="built_in">set</span> /a Mon = %TargetDate:~4,2%</span><br><span class="line">)</span><br><span class="line"><span class="keyword">if</span> %TargetDate:~6,1% == 0 (</span><br><span class="line"><span class="built_in">set</span> /a Day = %TargetDate:~7,1%</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line"><span class="built_in">set</span> /a Day = %TargetDate:~6,2%</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">:CheckLeapYear</span><br><span class="line">:: 检测是否是闰年[注2]</span><br><span class="line"><span class="built_in">set</span> /a DivisionBy4 = %Year%%%4</span><br><span class="line"><span class="built_in">set</span> /a DivisionBy100 = %Year%%%100</span><br><span class="line"><span class="built_in">set</span> /a DivisionBy400 = %Year%%%400</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> %DivisionBy4% == 0 <span class="built_in">set</span> /a IsLeapYear = 1 &amp; rem 能被4整除的是闰年</span><br><span class="line"><span class="keyword">if</span> %DivisionBy100% == 0 <span class="built_in">set</span> /a IsLeapYear = 0 &amp; rem 能被100整除的非闰年</span><br><span class="line"><span class="keyword">if</span> %DivisionBy400% == 0 <span class="built_in">set</span> /a IsLeapYear = 1 &amp; rem 能被400整除的是闰年</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a Month1 = 31</span><br><span class="line"><span class="built_in">set</span> /a Month2 = 28 + %IsLeapYear%</span><br><span class="line"><span class="built_in">set</span> /a Month3 = 31</span><br><span class="line"><span class="built_in">set</span> /a Month4 = 30</span><br><span class="line"><span class="built_in">set</span> /a Month5 = 31</span><br><span class="line"><span class="built_in">set</span> /a Month6 = 30</span><br><span class="line"><span class="built_in">set</span> /a Month7 = 31</span><br><span class="line"><span class="built_in">set</span> /a Month8 = 31</span><br><span class="line"><span class="built_in">set</span> /a Month9 = 30</span><br><span class="line"><span class="built_in">set</span> /a Month10= 31</span><br><span class="line"><span class="built_in">set</span> /a Month11= 30</span><br><span class="line"><span class="built_in">set</span> /a Month12= 31</span><br><span class="line"></span><br><span class="line">:: 检测范围是否有效[注3]</span><br><span class="line"><span class="keyword">if</span> %Year% lss 1 <span class="built_in">echo</span> 年份应该大于0 &amp; goto :Initialization</span><br><span class="line"><span class="keyword">if</span> %Year% gtr 9999 <span class="built_in">echo</span> 年份不能超过9999 &amp; goto :Initialization</span><br><span class="line"><span class="keyword">if</span> %Mon% lss 1 <span class="built_in">echo</span> 月份不能小于1 &amp; goto :Initialization</span><br><span class="line"><span class="keyword">if</span> %Mon% gtr 12 <span class="built_in">echo</span> 月份不能大于12 &amp; goto :Initialization</span><br><span class="line"><span class="keyword">if</span> %Day% lss 1 <span class="built_in">echo</span> 日数不能小于1 &amp; goto :Initialization</span><br><span class="line"><span class="keyword">if</span> %Day% gtr 31 <span class="built_in">echo</span> 日数不能大于31 &amp; goto :Initialization</span><br><span class="line"><span class="keyword">if</span> %IsLeapYear% == 0 (</span><br><span class="line"><span class="keyword">if</span> %Mon%%Day% == 229 <span class="built_in">echo</span> %Year%年的2月没有29日 &amp; goto :Initialization</span><br><span class="line">)</span><br><span class="line"><span class="keyword">if</span> %Day% gtr !Month%Mon%! <span class="built_in">echo</span> %Mon%月没有%Day%日 &amp; goto :Initialization</span><br><span class="line"></span><br><span class="line">:: 开始计算天数</span><br><span class="line"><span class="built_in">set</span> /a Year -= 1 &amp; rem 该年未结束，应减一年</span><br><span class="line"><span class="built_in">set</span> /a Day = %Day% + Year * 365 &amp; rem 一年365天</span><br><span class="line"><span class="built_in">set</span> /a Day = %Day% + Year/4 &amp; rem 每4年多一闰</span><br><span class="line"><span class="built_in">set</span> /a Day = %Day% - Year/100 &amp; rem 可每100年少一闰</span><br><span class="line"><span class="built_in">set</span> /a Day = %Day% + Year/400 &amp; rem 但每400年还多一闰</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /a Mon -= 1 &amp; rem 该月未结束，应减一月</span><br><span class="line"><span class="keyword">for</span> /l %%i <span class="keyword">in</span> (1,1,%Mon%) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> /a Day = !Day! + !Month%%i! &amp; rem 累计前几月的天数</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">:: 计算星期</span><br><span class="line"><span class="built_in">set</span> /a Week = %Day%%%7 &amp; rem 除7得余数</span><br><span class="line"><span class="built_in">set</span> WeekChars=日一二三四五六</span><br><span class="line"><span class="built_in">set</span> Week=!WeekChars:~%Week%,1! &amp; rem 换成汉字</span><br><span class="line"></span><br><span class="line">:: 显示结果</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">echo</span> %TargetDate:~0,4%年%TargetDate:~4,2%月%TargetDate:~6,2%日，是公元第%Day%天，星期%Week%</span><br><span class="line"><span class="built_in">echo</span> ___________________________________________</span><br><span class="line"><span class="built_in">echo</span>. &amp; pause&gt;nul &amp; goto :Initialization</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">注1 正如第三章关于 <span class="built_in">set</span> 为变量赋予数值型的值所说的，如果数值是以 0 开头的，那么该数值便是八进制的。这并不是我们所期望，因此对变量的首位数非零的检测是必要的。</span><br><span class="line"></span><br><span class="line">注2 闰年每隔四年来一次，这是众所周知的，但它并不完全正确。因为一年约为365.24219天，因此闰年将遵循于“四年一闰,百年不闰,四百年再闰”的规则。</span><br><span class="line"></span><br><span class="line">注3 每个月的天数并不固定，但它们都在28~31这个范围内。</span><br><span class="line"></span><br><span class="line">公元2738年11月28日是星期一，跟 Garfield 一样，我也讨厌星期一。</span><br><span class="line"></span><br><span class="line">========================================朴实的分割线========================================</span><br><span class="line"></span><br><span class="line">7.4 163邮箱登录器之不显示密码篇</span><br><span class="line"></span><br><span class="line">还记得我们在3.4节中所提到过的163信箱登录的问题吗，那是一个不错的小批处理。不过那个批处理存在着一个小小的缺憾：在输入密码时，密码会暴露无遗地直接显示在屏幕上，这对于注重个人隐私的您来说，显然是无法接受的。</span><br><span class="line"></span><br><span class="line">:::::::163邮箱登录器.bat::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">title 163邮箱登录器</span><br><span class="line">mode con cols=80 lines=25</span><br><span class="line">::设置窗口的尺寸</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /p username=用户名:</span><br><span class="line">cls</span><br><span class="line"></span><br><span class="line">chcp 437&gt;nul</span><br><span class="line">graftabl 936&gt;nul</span><br><span class="line">:: 转换代码页编号为 936</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> 用户名: %username%</span><br><span class="line"><span class="built_in">set</span> /p =密　码: &lt;nul</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> hP1X500P[PZBBBfh<span class="comment">#b##fXf-V@`$fPf]f3/f1/5++u5x&gt;in.com</span></span><br><span class="line">:: 创建一个神奇的二进制可执行文件</span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;tokens=*&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&#x27;in.com&#x27;</span>) <span class="keyword">do</span> <span class="built_in">set</span> password=%%i</span><br><span class="line">:: 在那个神奇的文件被执行完返回结果之前，不显示任何东西</span><br><span class="line">del in.com</span><br><span class="line"></span><br><span class="line">setlocal EnableDelayedExpansion</span><br><span class="line"><span class="keyword">for</span> /l %%i <span class="keyword">in</span> (0,1,255) <span class="keyword">do</span> (</span><br><span class="line"><span class="keyword">if</span> not <span class="string">&quot;!password:~%%i,1!&quot;</span>==<span class="string">&quot;&quot;</span> (</span><br><span class="line"><span class="built_in">set</span> /p =*&lt;nul</span><br><span class="line">) <span class="keyword">else</span> (</span><br><span class="line">endlocal</span><br><span class="line">goto :LoginMail</span><br><span class="line">)</span><br><span class="line">)</span><br><span class="line">endlocal</span><br><span class="line">:: 计算密码长度，然后显示相应个数的星号</span><br><span class="line"></span><br><span class="line">:LoginMail</span><br><span class="line"></span><br><span class="line">start <span class="string">&quot;正在登录邮箱&quot;</span> <span class="string">&quot;https://reg.163.com/logins.jsp?username=%username%&amp;password=%password%&amp;url=http://fm163.163.com/coremail/fcg/ntesdoor2&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">echo</span> 正在登录邮箱...</span><br><span class="line">ping -n 2 127.0&gt;nul</span><br><span class="line">:: 等待一小段时间</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">备注：本小节尚存在问题待解决(输入密码时使用退格会被当作字符记录下来，不输入任何东西则将显示Invalid keyboard code specified)……</span><br></pre></td></tr></table></figure>

<h2><span id="第八章番外篇">第八章：番外篇</span></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br></pre></td><td class="code"><pre><span class="line">标题很奇怪？其实，您可以把它当作一期OVA。</span><br><span class="line"></span><br><span class="line">8.1 批量十六进制二进制格式转换</span><br><span class="line"></span><br><span class="line">想把数据(流)以十六进制或二进制的形式显示出来？UltraEdit之类的编辑软件一定是首选。即使是要自己亲自转出来，C/C++等语言也会方便的多。如果您跟我一样选择使用批处理来实现的话，那么很好，很有挑战性。</span><br><span class="line"></span><br><span class="line">::::::DataFormatConvert.bat:::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"></span><br><span class="line">:Initialization</span><br><span class="line"><span class="built_in">set</span> TempFile=temp.tmp</span><br><span class="line"><span class="built_in">set</span> /a num = 0</span><br><span class="line"><span class="built_in">set</span> BinaryFormat=Bin</span><br><span class="line">:: 默认为二进制数字0或1显示</span><br><span class="line"><span class="built_in">set</span> ConvertOption=ConvertToHex</span><br><span class="line">:: 默认为十六进制转换</span><br><span class="line"><span class="built_in">set</span> ShowProgress=ShowProgressOn</span><br><span class="line">:: 默认为显示转换进度</span><br><span class="line"><span class="built_in">set</span> BackSpace=[将本文底部评论3中的退格符替换到此处]</span><br><span class="line">:: 退格符</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> Source=z%1</span><br><span class="line">:: 添加一个临时字符 z</span><br><span class="line"><span class="keyword">if</span> %Source:~1,1%z%Source:~-1%==<span class="string">&quot;z&quot;</span> <span class="built_in">set</span> Source=z%Source:~2,-1%</span><br><span class="line">:: 检查变量参数1的第一个和最后一个字符是否为 <span class="string">&quot;&quot;</span> ，是的话就去掉</span><br><span class="line"><span class="built_in">set</span> Source=%Source:~1%</span><br><span class="line">:: 去掉临时字符 z</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%Source%&quot;</span>==<span class="string">&quot;&quot;</span> goto :HelpInformation</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%Source%&quot;</span>==<span class="string">&quot;/?&quot;</span> goto :HelpInformation</span><br><span class="line"><span class="keyword">if</span> /i <span class="string">&quot;%Source%&quot;</span>==<span class="string">&quot;/help&quot;</span> goto :HelpInformation</span><br><span class="line"><span class="keyword">if</span> not exist <span class="string">&quot;%Source%&quot;</span> <span class="built_in">echo</span> 目标文件不存在 &amp; <span class="built_in">exit</span> /b 2</span><br><span class="line">:: 检查参数1 [注1]</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> /i <span class="string">&quot;%2&quot;</span>==<span class="string">&quot;/Bin&quot;</span> <span class="built_in">set</span> ConvertOption=ConvertToBin</span><br><span class="line"><span class="keyword">if</span> /i <span class="string">&quot;%3&quot;</span>==<span class="string">&quot;/Bin&quot;</span> <span class="built_in">set</span> ConvertOption=ConvertToBin</span><br><span class="line"><span class="keyword">if</span> /i <span class="string">&quot;%2&quot;</span>==<span class="string">&quot;/Block&quot;</span> <span class="built_in">set</span> ConvertOption=ConvertToBin &amp; <span class="built_in">set</span> BinaryFormat=BinBlock</span><br><span class="line"><span class="keyword">if</span> /i <span class="string">&quot;%3&quot;</span>==<span class="string">&quot;/Block&quot;</span> <span class="built_in">set</span> ConvertOption=ConvertToBin &amp; <span class="built_in">set</span> BinaryFormat=BinBlock</span><br><span class="line"><span class="keyword">if</span> /i <span class="string">&quot;%2&quot;</span>==<span class="string">&quot;/ProgressOff&quot;</span> <span class="built_in">set</span> ShowProgress=ShowProgressOff</span><br><span class="line"><span class="keyword">if</span> /i <span class="string">&quot;%3&quot;</span>==<span class="string">&quot;/ProgressOff&quot;</span> <span class="built_in">set</span> ShowProgress=ShowProgressOff</span><br><span class="line">:: 检查参数2和3 [注2]</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> %%i <span class="keyword">in</span> (<span class="string">&quot;%Source%&quot;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> SourceDrivePathName=%%~dpni</span><br><span class="line"><span class="built_in">set</span> FileSize=%%~zi</span><br><span class="line">)</span><br><span class="line">:: 获得源文件的 驱动器号+路径+标题名 ，以及文件大小。 d - drive, p - path, n - name, z - size</span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;%FileSize%&quot;</span>==<span class="string">&quot;0&quot;</span> <span class="built_in">echo</span> 目标文件为空 &amp; <span class="built_in">exit</span> /b 3</span><br><span class="line">del %TempFile% &gt;nul 2&gt;nul</span><br><span class="line">fsutil file createnew %TempFile% %FileSize% &gt;nul</span><br><span class="line">:: 创建一个大小与源文件一样的空白文件，该文件里的所有字节均为0x00</span><br><span class="line"></span><br><span class="line">goto :%ConvertOption%</span><br><span class="line"></span><br><span class="line">:ConvertToHex</span><br><span class="line">:: 十六进制转换</span><br><span class="line">setlocal EnableDelayedExpansion</span><br><span class="line"><span class="built_in">set</span> OutputFile=<span class="string">&quot;%SourceDrivePathName%.Hex.txt&quot;</span></span><br><span class="line"><span class="built_in">echo</span> 文件 %Source% 的十六进制格式:&gt;%OutputFile%</span><br><span class="line"><span class="built_in">echo</span>. &amp; <span class="built_in">echo</span> 正在转换为十六进制...</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;skip=1 tokens=1,3&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&#x27;fc /b %TempFile% &quot;%Source%&quot;&#x27;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> index=0x%%i</span><br><span class="line"><span class="built_in">set</span> index=!index:~0,-1!</span><br><span class="line"><span class="built_in">set</span> /a offset = !index! - !num!</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> /l %%n <span class="keyword">in</span> (1,1,!offset!) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> /p=00&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="built_in">set</span> /a num += 1</span><br><span class="line">)</span><br><span class="line"><span class="built_in">set</span> /p=%%j&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="built_in">set</span> /a num += 1</span><br><span class="line">call :%ShowProgress% !num!</span><br><span class="line">)</span><br><span class="line">:: 通过与大小相同内容空白的文件进行二进制对比，获得源文件的十六进制码 [注3]</span><br><span class="line">endlocal</span><br><span class="line">goto :ExitSuccess</span><br><span class="line"></span><br><span class="line">:ConvertToBin</span><br><span class="line">setlocal EnableDelayedExpansion</span><br><span class="line"><span class="built_in">set</span> OutputFile=<span class="string">&quot;%SourceDrivePathName%.%BinaryFormat%.txt&quot;</span></span><br><span class="line"><span class="built_in">echo</span> 文件 %Source% 的二进制格式:&gt;%OutputFile%</span><br><span class="line"><span class="built_in">echo</span>. &amp; <span class="built_in">echo</span> 正在转换为二进制...</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> /f <span class="string">&quot;skip=1 tokens=1,3&quot;</span> %%i <span class="keyword">in</span> (<span class="string">&#x27;fc /b %TempFile% &quot;%Source%&quot;&#x27;</span>) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> index=0x%%i</span><br><span class="line"><span class="built_in">set</span> index=!index:~0,-1!</span><br><span class="line"><span class="built_in">set</span> /a offset = !index! - !num!</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> /l %%n <span class="keyword">in</span> (1,1,!offset!) <span class="keyword">do</span> (</span><br><span class="line">call :HexTo%BinaryFormat% 0</span><br><span class="line">call :HexTo%BinaryFormat% 0</span><br><span class="line"><span class="built_in">set</span> /a num += 1</span><br><span class="line">)</span><br><span class="line"><span class="built_in">set</span> HexData=%%j</span><br><span class="line">call :HexTo%BinaryFormat% !HexData:~0,1!</span><br><span class="line">call :HexTo%BinaryFormat% !HexData:~1,1!</span><br><span class="line"><span class="built_in">set</span> /a num += 1</span><br><span class="line">call :%ShowProgress% !num!</span><br><span class="line">)</span><br><span class="line">endlocal</span><br><span class="line">goto :ExitSuccess</span><br><span class="line"></span><br><span class="line">:HexToBin</span><br><span class="line">:: 函数: HexToBin 用途: 以0或1的形式显示</span><br><span class="line"><span class="keyword">if</span> %1==0 <span class="built_in">set</span> /p=0000&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==1 <span class="built_in">set</span> /p=0001&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==2 <span class="built_in">set</span> /p=0010&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==3 <span class="built_in">set</span> /p=0011&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==4 <span class="built_in">set</span> /p=0100&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==5 <span class="built_in">set</span> /p=0101&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==6 <span class="built_in">set</span> /p=0110&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==7 <span class="built_in">set</span> /p=0111&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==8 <span class="built_in">set</span> /p=1000&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==9 <span class="built_in">set</span> /p=1001&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==A <span class="built_in">set</span> /p=1010&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==B <span class="built_in">set</span> /p=1011&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==C <span class="built_in">set</span> /p=1100&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==D <span class="built_in">set</span> /p=1101&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==E <span class="built_in">set</span> /p=1110&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==F <span class="built_in">set</span> /p=1111&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:HexToBinBlock</span><br><span class="line">:: 函数: HexToBinBlock 用途: 以　或■的形式显示[注3]</span><br><span class="line"><span class="keyword">if</span> %1==0 <span class="built_in">set</span> /p=　　　　&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==1 <span class="built_in">set</span> /p=　　　■&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==2 <span class="built_in">set</span> /p=　　■　&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==3 <span class="built_in">set</span> /p=　　■■&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==4 <span class="built_in">set</span> /p=　■　　&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==5 <span class="built_in">set</span> /p=　■　■&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==6 <span class="built_in">set</span> /p=　■■　&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==7 <span class="built_in">set</span> /p=　■■■&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==8 <span class="built_in">set</span> /p=■　　　&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==9 <span class="built_in">set</span> /p=■　　■&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==A <span class="built_in">set</span> /p=■　■　&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==B <span class="built_in">set</span> /p=■　■■&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==C <span class="built_in">set</span> /p=■■　　&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==D <span class="built_in">set</span> /p=■■　■&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==E <span class="built_in">set</span> /p=■■■　&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line"><span class="keyword">if</span> %1==F <span class="built_in">set</span> /p=■■■■&lt;nul&gt;&gt;%OutputFile%</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:ShowProgressOn</span><br><span class="line">:: 函数: ShowProgressOn 用途: 显示转换进度</span><br><span class="line"><span class="built_in">set</span> /a mod = %1 %% 50</span><br><span class="line"><span class="keyword">if</span> %mod% equ 0 (</span><br><span class="line"><span class="built_in">set</span> /a percent = %1 * 100 / %FileSize%</span><br><span class="line"><span class="built_in">set</span> /p=%BackSpace%%BackSpace%%BackSpace%&lt;nul</span><br><span class="line"><span class="built_in">set</span> /p=!percent!%%&lt;nul</span><br><span class="line">)</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:ShowProgressOff</span><br><span class="line">:: 函数: ShowProgressOff 用途: 不显示转换进度</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:HelpInformation</span><br><span class="line">:: 该批处理的帮助信息</span><br><span class="line"><span class="built_in">echo</span> 将文件转换为十六进制或二进制的格式。</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">echo</span> DataFormatConvert <span class="built_in">source</span> [/Hex ^| /Bin ^| /Block] [/ProgressOn ^| /ProgressOff]</span><br><span class="line"><span class="built_in">echo</span>.</span><br><span class="line"><span class="built_in">echo</span> <span class="built_in">source</span> 指定要转换的文件。</span><br><span class="line"><span class="built_in">echo</span> /Hex 表示转换为十六进制格式(默认)。</span><br><span class="line"><span class="built_in">echo</span> /Bin 表示转换为二进制格式。</span><br><span class="line"><span class="built_in">echo</span> /Block 表示转换为二进制格式并以方块的形式显示。</span><br><span class="line"><span class="built_in">echo</span> /ProgressOn　显示转换进度(默认)。</span><br><span class="line"><span class="built_in">echo</span> /ProgressOff 关闭显示进度。</span><br><span class="line"><span class="built_in">exit</span> /b 1</span><br><span class="line"></span><br><span class="line">:ExitSuccess</span><br><span class="line">:: 成功完成并退出</span><br><span class="line">del %TempFile% &gt;nul</span><br><span class="line"><span class="built_in">set</span> /p=%BackSpace%%BackSpace%%BackSpace%%BackSpace%&lt;nul</span><br><span class="line"><span class="built_in">echo</span> 转换完成!</span><br><span class="line"><span class="built_in">exit</span> /b 0</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">注1 <span class="built_in">exit</span> 的参数 /b ，可以退出当前批处理脚本而不退出调用它的 CMD.EXE。如果从一个批处理脚本外执行，则会退出 CMD.EXE 。<span class="built_in">exit</span> 的第二个参数 exitCode 是一个数字号码，如果使用了参数 /b ，这个数字号码将成为该批处理退出的 ErrorLevel 。如果退出 CMD.EXE ，则用那个数字设置过程退出代码。</span><br><span class="line"></span><br><span class="line">注2 看到该批处理这些可使用的参数，您可能已经发现了，该批处理可以作为一条外部命令来使用，如果您把它放到 %SystemRoot%\system32 或 %SystemRoot% 路径下的话。默认情况下，%SystemRoot%\system32 和 %SystemRoot% 都是环境变量 PATH 的值之一(在 我的电脑-&gt;属性-&gt;高级-&gt;环境变量 中可以查看并设置相关环境变量)。</span><br><span class="line"></span><br><span class="line">注3 源文件通过与其大小相同内容全为0x00的文件进行对比后，可以将源文件中所有与0x00不同的字节内容以十六进制码的形式表达出来。同时，还能得到每个不同字节所在的字节数位置，通过这个也便确定了源文件中0x00的内容。</span><br><span class="line"></span><br><span class="line">正如注2中所说的，如果该批处理被放到了环境变量 PATH 中的路径中，则它可以作为外部命令来为您工作。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">DataFormatConvert 的执行示意</span><br><span class="line"></span><br><span class="line">十六进制或二进制转换成功的文件将被写到与源文件相同的路径中。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">经DataFormatConvert 十六进制转换后的文件</span><br><span class="line">对于计算机而言，十六进制码(或者说0和1)才能表达出它们自己的想法，尽管这些对我们来说犹如天书一般。</span><br><span class="line"></span><br><span class="line">========================================朴实的分割线========================================</span><br><span class="line"></span><br><span class="line">8.2 别挤，自启动的排队来</span><br><span class="line"></span><br><span class="line">被放入到自启动项里的程序，从某种意义上来说，在被开启的时候确实简化了用户的操作。然而，在开机后众多的程序就像春运时期挤火车一样的蜂拥而上，这并不是我们所期望的。请不要问我为什么给这个批处理起了个如此无理的名称，所有放在 C:\WINDOWS 下的批处理我都会以小写字母 z 打头，方便管理。</span><br><span class="line"></span><br><span class="line">::::::::::zLauncher.bat:::::::::</span><br><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line">setlocal enabledelayedexpansion</span><br><span class="line">title Launching...</span><br><span class="line"><span class="built_in">set</span> /a ShortDelay = 6</span><br><span class="line"><span class="built_in">set</span> BackSpace=[将本文底部评论3中的退格符替换到此处]</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> /p choice=LaunchOptions:</span><br><span class="line"><span class="keyword">if</span> not <span class="string">&quot;%choice%&quot;</span>==<span class="string">&quot;&quot;</span> goto :CustomLaunch</span><br><span class="line"></span><br><span class="line">:BasicLaunch</span><br><span class="line">call :LaunchItem1</span><br><span class="line">call :LaunchItem2</span><br><span class="line">call :LaunchItem3</span><br><span class="line">call :LaunchItem4</span><br><span class="line">call :LaunchItem5</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:CustomLaunch</span><br><span class="line"><span class="keyword">for</span> /l %%i <span class="keyword">in</span> (0 1 9) <span class="keyword">do</span> (</span><br><span class="line"><span class="built_in">set</span> <span class="string">&quot;choice_=!choice:~%%i,1!&quot;</span></span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;!choice_!&quot;</span>==<span class="string">&quot;&quot;</span> <span class="built_in">echo</span> All Launched! &amp; goto :EOF</span><br><span class="line"><span class="keyword">if</span> !choice_! gtr 9 <span class="built_in">echo</span> Unknown Define &amp; goto :EOF</span><br><span class="line"><span class="keyword">if</span> !choice_! lss 0 <span class="built_in">echo</span> Unknown Define &amp; goto :EOF</span><br><span class="line">call :LaunchItem!choice_!</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">:LaunchItem1</span><br><span class="line">start <span class="string">&quot;OE&quot;</span> <span class="string">&quot;C:\Program Files\Outlook Express\msimn.exe&quot;</span></span><br><span class="line">call :IsItemLaunchedSuccessful OE</span><br><span class="line">goto :EOF</span><br><span class="line">:LaunchItem2</span><br><span class="line">start <span class="string">&quot;Skype&quot;</span> <span class="string">&quot;D:\Program Files\Skype\Phone\Skype.exe&quot;</span> /nosplash /minimized</span><br><span class="line">call :IsItemLaunchedSuccessful Skype</span><br><span class="line">goto :EOF</span><br><span class="line">:LaunchItem3</span><br><span class="line">Start <span class="string">&quot;QQ&quot;</span> <span class="string">&quot;D:\Program Files\Tencent\QQ\QQ.exe&quot;</span></span><br><span class="line">call :IsItemLaunchedSuccessful QQ</span><br><span class="line">goto :EOF</span><br><span class="line">:LaunchItem4</span><br><span class="line">start <span class="string">&quot;BaiduHi&quot;</span> /min <span class="string">&quot;D:\Program Files\baidu\Baidu Hi\BaiduHi.exe&quot;</span></span><br><span class="line">call :IsItemLaunchedSuccessful BaiduHi</span><br><span class="line">goto :EOF</span><br><span class="line">:LaunchItem5</span><br><span class="line">start <span class="string">&quot;Thunder&quot;</span> /min <span class="string">&quot;D:\Program Files\Thunder Network\Thunder\Thunder.exe&quot;</span></span><br><span class="line">call :IsItemLaunchedSuccessful Thunder</span><br><span class="line">goto :EOF</span><br><span class="line">:LaunchItem6</span><br><span class="line">start <span class="string">&quot;Tudou&quot;</span> /min <span class="string">&quot;D:\Program Files\Tudou\iTudou\iTudou.exe&quot;</span></span><br><span class="line">call :IsItemLaunchedSuccessful Tudou</span><br><span class="line">goto :EOF</span><br><span class="line">:LaunchItem7</span><br><span class="line">start <span class="string">&quot;MSN&quot;</span> /min <span class="string">&quot;C:\Program Files\MSN Messenger\msnmsgr.exe&quot;</span></span><br><span class="line">call :IsItemLaunchedSuccessful MSN</span><br><span class="line">goto :EOF</span><br><span class="line"></span><br><span class="line">:IsItemLaunchedSuccessful</span><br><span class="line"><span class="built_in">set</span> /p =Launching %1&lt;nul &amp; title Launching %1</span><br><span class="line"><span class="keyword">if</span> ERRORLEVEL 0 (</span><br><span class="line"><span class="keyword">for</span> /l %%i <span class="keyword">in</span> (1,1,4) <span class="keyword">do</span> (</span><br><span class="line">ping -n %ShortDelay% 127.1&gt;nul</span><br><span class="line"><span class="built_in">set</span> /p =.&lt;nul</span><br><span class="line">)</span><br><span class="line">)</span><br><span class="line"><span class="keyword">for</span> /l %%i <span class="keyword">in</span> (1,1,32) <span class="keyword">do</span> <span class="built_in">set</span> /p =%BackSpace%&lt;nul</span><br><span class="line"><span class="built_in">echo</span> %1 Launched</span><br><span class="line">title %1 Launched</span><br><span class="line">ping -n %ShortDelay% 127.1&gt;nul</span><br><span class="line">goto :EOF</span><br><span class="line">::::::::::::::::::::::::::::::::</span><br><span class="line"></span><br><span class="line">看得出来，这是一个科技含量很低的批处理，仅仅是在 start 每个程序之间将其挂起一段时间来避免众多程序同时启动所带来的负荷。当然，为了使它能够发挥作用，您得亲自配置它所要启动的程序，以及程序所在的路径才行。</span><br></pre></td></tr></table></figure>


            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='http://link.hhtjim.com/163/1485858993.mp3'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='false'
        data-ae='false'
        data-ci=''
        data-cs=''
        data-r=''
        data-o=''
        data-a=''
        data-d='false'
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
